blob: 9273216f22fce7890cee024290b7d35a5952ab25 [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,
62 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000063 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020064 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010065 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010067 [snd_soc_dapm_dai_in] = 3,
68 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010069 [snd_soc_dapm_aif_in] = 3,
70 [snd_soc_dapm_aif_out] = 3,
71 [snd_soc_dapm_mic] = 4,
72 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000073 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_value_mux] = 5,
75 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020076 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010077 [snd_soc_dapm_mixer] = 7,
78 [snd_soc_dapm_mixer_named_ctl] = 7,
79 [snd_soc_dapm_pga] = 8,
80 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060081 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010082 [snd_soc_dapm_hp] = 10,
83 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 10,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020085 [snd_soc_dapm_kcontrol] = 11,
86 [snd_soc_dapm_post] = 12,
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,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200112 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100113 [snd_soc_dapm_regulator_supply] = 12,
114 [snd_soc_dapm_supply] = 12,
115 [snd_soc_dapm_post] = 13,
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
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100374static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
375{
376 if (w->codec)
377 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100378 else if (w->platform)
379 return snd_soc_platform_read(w->platform, reg);
380
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000381 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100382 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100383}
384
385static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
386{
387 if (w->codec)
388 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100389 else if (w->platform)
390 return snd_soc_platform_write(w->platform, reg, val);
391
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000392 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100393 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100394}
395
Liam Girdwood49575fb52012-03-06 18:16:19 +0000396static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
397{
Mark Browne06ab3b2012-03-06 23:58:22 +0000398 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000399 mutex_lock(&w->codec->mutex);
400 else if (w->platform)
401 mutex_lock(&w->platform->mutex);
402}
403
404static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
405{
Mark Browne06ab3b2012-03-06 23:58:22 +0000406 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000407 mutex_unlock(&w->codec->mutex);
408 else if (w->platform)
409 mutex_unlock(&w->platform->mutex);
410}
411
412static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100413 unsigned short reg, unsigned int mask, unsigned int value)
414{
Mark Brown8a713da2011-12-03 12:33:55 +0000415 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100416 unsigned int old, new;
417 int ret;
418
Mark Brown8a713da2011-12-03 12:33:55 +0000419 if (w->codec && w->codec->using_regmap) {
420 ret = regmap_update_bits_check(w->codec->control_data,
421 reg, mask, value, &change);
422 if (ret != 0)
423 return ret;
424 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000425 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000426 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000427 if (ret < 0) {
428 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100429 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000430 }
Mark Brown8a713da2011-12-03 12:33:55 +0000431
432 old = ret;
433 new = (old & ~mask) | (value & mask);
434 change = old != new;
435 if (change) {
436 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000437 if (ret < 0) {
438 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000439 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000440 }
Mark Brown8a713da2011-12-03 12:33:55 +0000441 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000442 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100443 }
444
445 return change;
446}
447
Mark Brown452c5ea2009-05-17 21:41:23 +0100448/**
449 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800450 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100451 * @level: level to configure
452 *
453 * Configure the bias (power) levels for the SoC audio device.
454 *
455 * Returns 0 for success else error.
456 */
Mark Browned5a4c42011-02-18 11:12:42 -0800457static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200458 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100459{
Mark Browned5a4c42011-02-18 11:12:42 -0800460 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100461 int ret = 0;
462
Mark Brown84e90932010-11-04 00:07:02 -0400463 trace_snd_soc_bias_level_start(card, level);
464
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100466 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100467 if (ret != 0)
468 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100469
Mark Browncc4c6702011-06-06 19:03:34 +0100470 if (dapm->codec) {
471 if (dapm->codec->driver->set_bias_level)
472 ret = dapm->codec->driver->set_bias_level(dapm->codec,
473 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100474 else
475 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100476 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100477 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100478 }
Liam Girdwood41231282012-07-06 16:56:16 +0100479
Mark Brown171ec6b2011-06-06 18:15:19 +0100480 if (ret != 0)
481 goto out;
482
483 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100484 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100485out:
Mark Brown84e90932010-11-04 00:07:02 -0400486 trace_snd_soc_bias_level_done(card, level);
487
Mark Brown452c5ea2009-05-17 21:41:23 +0100488 return ret;
489}
490
Richard Purdie2b97eab2006-10-06 18:32:18 +0200491/* set up initial codec paths */
492static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
493 struct snd_soc_dapm_path *p, int i)
494{
495 switch (w->id) {
496 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000497 case snd_soc_dapm_mixer:
498 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100500 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600501 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200502 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400503 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100504 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400505 unsigned int mask = (1 << fls(max)) - 1;
506 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200508 if (reg != SND_SOC_NOPM) {
509 val = soc_widget_read(w, reg);
510 val = (val >> shift) & mask;
511 if (invert)
512 val = max - val;
513 p->connect = !!val;
514 } else {
515 p->connect = 0;
516 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518 }
519 break;
520 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600521 struct soc_enum *e = (struct soc_enum *)
522 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200523 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200524
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100525 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200526 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200528 if (item < e->max && !strcmp(p->name, e->texts[item]))
529 p->connect = 1;
530 else
531 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 }
533 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000534 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600535 struct soc_enum *e = (struct soc_enum *)
536 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000537
538 p->connect = 0;
539 /* since a virtual mux has no backing registers to
540 * decide which path to connect, it will try to match
541 * with the first enumeration. This is to ensure
542 * that the default mux choice (the first) will be
543 * correctly powered up during initialization.
544 */
545 if (!strcmp(p->name, e->texts[0]))
546 p->connect = 1;
547 }
548 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200549 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200550 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600551 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200552 int val, item;
553
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100554 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200555 val = (val >> e->shift_l) & e->mask;
556 for (item = 0; item < e->max; item++) {
557 if (val == e->values[item])
558 break;
559 }
560
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200561 if (item < e->max && !strcmp(p->name, e->texts[item]))
562 p->connect = 1;
563 else
564 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200565 }
566 break;
Mark Brown56563102011-10-03 22:41:09 +0100567 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200568 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600569 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 case snd_soc_dapm_output:
571 case snd_soc_dapm_adc:
572 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000573 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200574 case snd_soc_dapm_dac:
575 case snd_soc_dapm_micbias:
576 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100577 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000578 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200579 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100580 case snd_soc_dapm_aif_in:
581 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100582 case snd_soc_dapm_dai_in:
583 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200584 case snd_soc_dapm_hp:
585 case snd_soc_dapm_mic:
586 case snd_soc_dapm_spk:
587 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100588 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200589 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100590 p->connect = 1;
591 break;
592 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200593 case snd_soc_dapm_pre:
594 case snd_soc_dapm_post:
595 p->connect = 0;
596 break;
597 }
598}
599
Mark Brown74b8f952009-06-06 11:26:15 +0100600/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200601static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
603 struct snd_soc_dapm_path *path, const char *control_name,
604 const struct snd_kcontrol_new *kcontrol)
605{
606 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
607 int i;
608
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100609 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200610 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200611 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612 list_add(&path->list_sink, &dest->sources);
613 list_add(&path->list_source, &src->sinks);
614 path->name = (char*)e->texts[i];
615 dapm_set_path_status(dest, path, 0);
616 return 0;
617 }
618 }
619
620 return -ENODEV;
621}
622
Mark Brown74b8f952009-06-06 11:26:15 +0100623/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200624static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200625 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
626 struct snd_soc_dapm_path *path, const char *control_name)
627{
628 int i;
629
630 /* search for mixer kcontrol */
631 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600632 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200633 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200634 list_add(&path->list_sink, &dest->sources);
635 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600636 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200637 dapm_set_path_status(dest, path, i);
638 return 0;
639 }
640 }
641 return -ENODEV;
642}
643
Stephen Warrenaf468002011-04-28 17:38:01 -0600644static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600645 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600646 const struct snd_kcontrol_new *kcontrol_new,
647 struct snd_kcontrol **kcontrol)
648{
649 struct snd_soc_dapm_widget *w;
650 int i;
651
652 *kcontrol = NULL;
653
654 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600655 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
656 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600657 for (i = 0; i < w->num_kcontrols; i++) {
658 if (&w->kcontrol_news[i] == kcontrol_new) {
659 if (w->kcontrols)
660 *kcontrol = w->kcontrols[i];
661 return 1;
662 }
663 }
664 }
665
666 return 0;
667}
668
Stephen Warren85762e72013-03-29 15:40:10 -0600669/*
670 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
671 * create it. Either way, add the widget into the control's widget list
672 */
673static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100674 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200675{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200676 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000677 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000678 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600679 size_t prefix_len;
680 int shared;
681 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600682 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600683 char *long_name;
684 const char *name;
685 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000686
687 if (dapm->codec)
688 prefix = dapm->codec->name_prefix;
689 else
690 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000692 if (prefix)
693 prefix_len = strlen(prefix) + 1;
694 else
695 prefix_len = 0;
696
Stephen Warren85762e72013-03-29 15:40:10 -0600697 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
698 &kcontrol);
699
Stephen Warren85762e72013-03-29 15:40:10 -0600700 if (!kcontrol) {
701 if (shared) {
702 wname_in_long_name = false;
703 kcname_in_long_name = true;
704 } else {
705 switch (w->id) {
706 case snd_soc_dapm_switch:
707 case snd_soc_dapm_mixer:
708 wname_in_long_name = true;
709 kcname_in_long_name = true;
710 break;
711 case snd_soc_dapm_mixer_named_ctl:
712 wname_in_long_name = false;
713 kcname_in_long_name = true;
714 break;
715 case snd_soc_dapm_mux:
716 case snd_soc_dapm_virt_mux:
717 case snd_soc_dapm_value_mux:
718 wname_in_long_name = true;
719 kcname_in_long_name = false;
720 break;
721 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600722 return -EINVAL;
723 }
724 }
725
726 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600727 /*
728 * The control will get a prefix from the control
729 * creation process but we're also using the same
730 * prefix for widgets so cut the prefix off the
731 * front of the widget name.
732 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200733 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600734 w->name + prefix_len,
735 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200736 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200737 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600738
739 name = long_name;
740 } else if (wname_in_long_name) {
741 long_name = NULL;
742 name = w->name + prefix_len;
743 } else {
744 long_name = NULL;
745 name = w->kcontrol_news[kci].name;
746 }
747
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200748 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600749 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200750 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200751 if (!kcontrol)
752 return -ENOMEM;
753 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200754
755 ret = dapm_kcontrol_data_alloc(w, kcontrol);
756 if (ret) {
757 snd_ctl_free_one(kcontrol);
758 return ret;
759 }
760
Stephen Warren85762e72013-03-29 15:40:10 -0600761 ret = snd_ctl_add(card, kcontrol);
762 if (ret < 0) {
763 dev_err(dapm->dev,
764 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
765 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600766 return ret;
767 }
Stephen Warren85762e72013-03-29 15:40:10 -0600768 }
769
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200770 ret = dapm_kcontrol_add_widget(kcontrol, w);
771 if (ret)
772 return ret;
773
Stephen Warren85762e72013-03-29 15:40:10 -0600774 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600775
776 return 0;
777}
778
779/* create new dapm mixer control */
780static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
781{
782 int i, ret;
783 struct snd_soc_dapm_path *path;
784
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 /* add kcontrol */
786 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200787 /* match name */
788 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600790 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791 continue;
792
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200793 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200794 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200795 continue;
796 }
797
Mark Brown946d92a2013-08-12 23:28:42 +0100798 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600799 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100801
802 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803 }
804 }
Stephen Warren85762e72013-03-29 15:40:10 -0600805
806 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807}
808
809/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200810static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200812 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600813 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600814 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200815
Stephen Warrenaf468002011-04-28 17:38:01 -0600816 if (w->num_kcontrols != 1) {
817 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000818 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600819 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820 return -EINVAL;
821 }
822
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200823 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600824 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
825 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600826 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200827
Mark Brown946d92a2013-08-12 23:28:42 +0100828 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600829 if (ret < 0)
830 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600831
Richard Purdie2b97eab2006-10-06 18:32:18 +0200832 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200833 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834
Stephen Warrenaf468002011-04-28 17:38:01 -0600835 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200836}
837
838/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200839static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840{
Mark Browna6c65732010-03-03 17:45:21 +0000841 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200842 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000843 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844
Mark Browna6c65732010-03-03 17:45:21 +0000845 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200846}
847
848/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100849static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
850 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851{
852 struct snd_soc_dapm_path *p;
853
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100854 list_for_each_entry(p, sink, list_source) {
855 if (p->walked) {
856 p->walked = 0;
857 dapm_clear_walk_output(dapm, &p->sink->sinks);
858 }
859 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860}
861
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100862static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
863 struct list_head *source)
864{
865 struct snd_soc_dapm_path *p;
866
867 list_for_each_entry(p, source, list_sink) {
868 if (p->walked) {
869 p->walked = 0;
870 dapm_clear_walk_input(dapm, &p->source->sources);
871 }
872 }
873}
874
875
Mark Brown99497882010-05-07 20:24:05 +0100876/* We implement power down on suspend by checking the power state of
877 * the ALSA card - when we are suspending the ALSA state for the card
878 * is set to D3.
879 */
880static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
881{
Mark Brown12ea2c72011-03-02 18:17:32 +0000882 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100883
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100885 case SNDRV_CTL_POWER_D3hot:
886 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100887 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000888 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200889 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100890 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100891 default:
892 return 1;
893 }
894}
895
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100896/* add widget to list if it's not already in the list */
897static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
898 struct snd_soc_dapm_widget *w)
899{
900 struct snd_soc_dapm_widget_list *wlist;
901 int wlistsize, wlistentries, i;
902
903 if (*list == NULL)
904 return -EINVAL;
905
906 wlist = *list;
907
908 /* is this widget already in the list */
909 for (i = 0; i < wlist->num_widgets; i++) {
910 if (wlist->widgets[i] == w)
911 return 0;
912 }
913
914 /* allocate some new space */
915 wlistentries = wlist->num_widgets + 1;
916 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
917 wlistentries * sizeof(struct snd_soc_dapm_widget *);
918 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
919 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000920 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100921 w->name);
922 return -ENOMEM;
923 }
924 wlist = *list;
925
926 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000927 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100928 w->name, wlist->num_widgets);
929
930 wlist->widgets[wlist->num_widgets] = w;
931 wlist->num_widgets++;
932 return 1;
933}
934
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935/*
936 * Recursively check for a completed path to an active or physically connected
937 * output widget. Returns number of complete paths.
938 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100939static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
940 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941{
942 struct snd_soc_dapm_path *path;
943 int con = 0;
944
Mark Brown024dc072011-10-09 11:52:05 +0100945 if (widget->outputs >= 0)
946 return widget->outputs;
947
Mark Brownde02d072011-09-20 21:43:24 +0100948 DAPM_UPDATE_STAT(widget, path_checks);
949
Mark Brown62ea8742012-01-21 21:14:48 +0000950 switch (widget->id) {
951 case snd_soc_dapm_supply:
952 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200953 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200954 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100955 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000956 default:
957 break;
958 }
Mark Brown246d0a12009-04-22 18:24:55 +0100959
Mark Brown010ff262009-08-17 17:39:22 +0100960 switch (widget->id) {
961 case snd_soc_dapm_adc:
962 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100963 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100964 if (widget->active) {
965 widget->outputs = snd_soc_dapm_suspend_check(widget);
966 return widget->outputs;
967 }
Mark Brown010ff262009-08-17 17:39:22 +0100968 default:
969 break;
970 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971
972 if (widget->connected) {
973 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100974 if (widget->id == snd_soc_dapm_output && !widget->ext) {
975 widget->outputs = snd_soc_dapm_suspend_check(widget);
976 return widget->outputs;
977 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200978
979 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100980 if (widget->id == snd_soc_dapm_hp ||
981 widget->id == snd_soc_dapm_spk ||
982 (widget->id == snd_soc_dapm_line &&
983 !list_empty(&widget->sources))) {
984 widget->outputs = snd_soc_dapm_suspend_check(widget);
985 return widget->outputs;
986 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200987 }
988
989 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100990 DAPM_UPDATE_STAT(widget, neighbour_checks);
991
Mark Brownbf3a9e12011-06-13 16:42:29 +0100992 if (path->weak)
993 continue;
994
Mark Brown8af294b2013-02-22 17:48:15 +0000995 if (path->walking)
996 return 1;
997
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998 if (path->walked)
999 continue;
1000
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001001 trace_snd_soc_dapm_output_path(widget, path);
1002
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003 if (path->sink && path->connect) {
1004 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001005 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001006
1007 /* do we need to add this widget to the list ? */
1008 if (list) {
1009 int err;
1010 err = dapm_list_add_widget(list, path->sink);
1011 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001012 dev_err(widget->dapm->dev,
1013 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001014 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001015 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001016 return con;
1017 }
1018 }
1019
1020 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001021
1022 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 }
1024 }
1025
Mark Brown024dc072011-10-09 11:52:05 +01001026 widget->outputs = con;
1027
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028 return con;
1029}
1030
1031/*
1032 * Recursively check for a completed path to an active or physically connected
1033 * input widget. Returns number of complete paths.
1034 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001035static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1036 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001037{
1038 struct snd_soc_dapm_path *path;
1039 int con = 0;
1040
Mark Brown024dc072011-10-09 11:52:05 +01001041 if (widget->inputs >= 0)
1042 return widget->inputs;
1043
Mark Brownde02d072011-09-20 21:43:24 +01001044 DAPM_UPDATE_STAT(widget, path_checks);
1045
Mark Brown62ea8742012-01-21 21:14:48 +00001046 switch (widget->id) {
1047 case snd_soc_dapm_supply:
1048 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001049 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001050 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001051 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001052 default:
1053 break;
1054 }
Mark Brown246d0a12009-04-22 18:24:55 +01001055
Richard Purdie2b97eab2006-10-06 18:32:18 +02001056 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001057 switch (widget->id) {
1058 case snd_soc_dapm_dac:
1059 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001060 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001061 if (widget->active) {
1062 widget->inputs = snd_soc_dapm_suspend_check(widget);
1063 return widget->inputs;
1064 }
Mark Brown010ff262009-08-17 17:39:22 +01001065 default:
1066 break;
1067 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001068
1069 if (widget->connected) {
1070 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001071 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1072 widget->inputs = snd_soc_dapm_suspend_check(widget);
1073 return widget->inputs;
1074 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001075
1076 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001077 if (widget->id == snd_soc_dapm_vmid) {
1078 widget->inputs = snd_soc_dapm_suspend_check(widget);
1079 return widget->inputs;
1080 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001081
1082 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001083 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001084 (widget->id == snd_soc_dapm_line &&
1085 !list_empty(&widget->sinks))) {
1086 widget->inputs = snd_soc_dapm_suspend_check(widget);
1087 return widget->inputs;
1088 }
1089
Mark Brown1ab97c82011-11-27 16:21:51 +00001090 /* signal generator */
1091 if (widget->id == snd_soc_dapm_siggen) {
1092 widget->inputs = snd_soc_dapm_suspend_check(widget);
1093 return widget->inputs;
1094 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001095 }
1096
1097 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001098 DAPM_UPDATE_STAT(widget, neighbour_checks);
1099
Mark Brownbf3a9e12011-06-13 16:42:29 +01001100 if (path->weak)
1101 continue;
1102
Mark Brown8af294b2013-02-22 17:48:15 +00001103 if (path->walking)
1104 return 1;
1105
Richard Purdie2b97eab2006-10-06 18:32:18 +02001106 if (path->walked)
1107 continue;
1108
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001109 trace_snd_soc_dapm_input_path(widget, path);
1110
Richard Purdie2b97eab2006-10-06 18:32:18 +02001111 if (path->source && path->connect) {
1112 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001113 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001114
1115 /* do we need to add this widget to the list ? */
1116 if (list) {
1117 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001118 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001120 dev_err(widget->dapm->dev,
1121 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001122 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001123 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001124 return con;
1125 }
1126 }
1127
1128 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001129
1130 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 }
1132 }
1133
Mark Brown024dc072011-10-09 11:52:05 +01001134 widget->inputs = con;
1135
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136 return con;
1137}
1138
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001139/**
1140 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1141 * @dai: the soc DAI.
1142 * @stream: stream direction.
1143 * @list: list of active widgets for this stream.
1144 *
1145 * Queries DAPM graph as to whether an valid audio stream path exists for
1146 * the initial stream specified by name. This takes into account
1147 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1148 *
1149 * Returns the number of valid paths or negative error.
1150 */
1151int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1152 struct snd_soc_dapm_widget_list **list)
1153{
1154 struct snd_soc_card *card = dai->card;
1155 int paths;
1156
1157 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1158 dapm_reset(card);
1159
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001160 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001161 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001162 dapm_clear_walk_output(&card->dapm,
1163 &dai->playback_widget->sinks);
1164 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001165 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001166 dapm_clear_walk_input(&card->dapm,
1167 &dai->capture_widget->sources);
1168 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001169
1170 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001171 mutex_unlock(&card->dapm_mutex);
1172
1173 return paths;
1174}
1175
Richard Purdie2b97eab2006-10-06 18:32:18 +02001176/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001177 * Handler for generic register modifier widget.
1178 */
1179int dapm_reg_event(struct snd_soc_dapm_widget *w,
1180 struct snd_kcontrol *kcontrol, int event)
1181{
1182 unsigned int val;
1183
1184 if (SND_SOC_DAPM_EVENT_ON(event))
1185 val = w->on_val;
1186 else
1187 val = w->off_val;
1188
Liam Girdwood49575fb52012-03-06 18:16:19 +00001189 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001190 w->mask << w->shift, val << w->shift);
1191
1192 return 0;
1193}
Mark Brown11589412008-07-29 11:42:23 +01001194EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001195
Mark Brown62ea8742012-01-21 21:14:48 +00001196/*
1197 * Handler for regulator supply widget.
1198 */
1199int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1200 struct snd_kcontrol *kcontrol, int event)
1201{
Mark Brownc05b84d2012-09-07 12:57:11 +08001202 int ret;
1203
1204 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001205 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001206 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001207 if (ret != 0)
1208 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001209 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001210 w->name, ret);
1211 }
1212
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001213 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001214 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001215 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001216 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001217 if (ret != 0)
1218 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001219 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001220 w->name, ret);
1221 }
1222
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001223 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001224 }
Mark Brown62ea8742012-01-21 21:14:48 +00001225}
1226EXPORT_SYMBOL_GPL(dapm_regulator_event);
1227
Ola Liljad7e7eb92012-05-24 15:26:25 +02001228/*
1229 * Handler for clock supply widget.
1230 */
1231int dapm_clock_event(struct snd_soc_dapm_widget *w,
1232 struct snd_kcontrol *kcontrol, int event)
1233{
1234 if (!w->clk)
1235 return -EIO;
1236
Mark Brownec029952012-06-04 08:16:20 +01001237#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001238 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001239 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001240 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001241 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001242 return 0;
1243 }
Mark Brownec029952012-06-04 08:16:20 +01001244#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001245 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001246}
1247EXPORT_SYMBOL_GPL(dapm_clock_event);
1248
Mark Brownd8050022011-09-28 18:28:23 +01001249static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1250{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001251 if (w->power_checked)
1252 return w->new_power;
1253
Mark Brownd8050022011-09-28 18:28:23 +01001254 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001255 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001256 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001257 w->new_power = w->power_check(w);
1258
1259 w->power_checked = true;
1260
1261 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001262}
1263
Mark Browncd0f2d42009-04-20 16:56:59 +01001264/* Generic check to see if a widget should be powered.
1265 */
1266static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1267{
1268 int in, out;
1269
Mark Brownde02d072011-09-20 21:43:24 +01001270 DAPM_UPDATE_STAT(w, power_checks);
1271
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001272 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001273 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001274 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001275 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001276 return out != 0 && in != 0;
1277}
1278
Mark Brown6ea31b92009-04-20 17:15:41 +01001279/* Check to see if an ADC has power */
1280static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1281{
1282 int in;
1283
Mark Brownde02d072011-09-20 21:43:24 +01001284 DAPM_UPDATE_STAT(w, power_checks);
1285
Mark Brown6ea31b92009-04-20 17:15:41 +01001286 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001287 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001288 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001289 return in != 0;
1290 } else {
1291 return dapm_generic_check_power(w);
1292 }
1293}
1294
1295/* Check to see if a DAC has power */
1296static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1297{
1298 int out;
1299
Mark Brownde02d072011-09-20 21:43:24 +01001300 DAPM_UPDATE_STAT(w, power_checks);
1301
Mark Brown6ea31b92009-04-20 17:15:41 +01001302 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001303 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001304 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001305 return out != 0;
1306 } else {
1307 return dapm_generic_check_power(w);
1308 }
1309}
1310
Mark Brown246d0a12009-04-22 18:24:55 +01001311/* Check to see if a power supply is needed */
1312static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1313{
1314 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001315
Mark Brownde02d072011-09-20 21:43:24 +01001316 DAPM_UPDATE_STAT(w, power_checks);
1317
Mark Brown246d0a12009-04-22 18:24:55 +01001318 /* Check if one of our outputs is connected */
1319 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001320 DAPM_UPDATE_STAT(w, neighbour_checks);
1321
Mark Brownbf3a9e12011-06-13 16:42:29 +01001322 if (path->weak)
1323 continue;
1324
Mark Brown215edda2009-09-08 18:59:05 +01001325 if (path->connected &&
1326 !path->connected(path->source, path->sink))
1327 continue;
1328
Mark Brown30173582011-02-11 11:42:19 +00001329 if (!path->sink)
1330 continue;
1331
Mark Brownf68d7e12011-10-04 22:57:50 +01001332 if (dapm_widget_power_check(path->sink))
1333 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001334 }
1335
Mark Brownf68d7e12011-10-04 22:57:50 +01001336 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001337}
1338
Mark Brown35c64bc2011-09-28 18:23:53 +01001339static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1340{
1341 return 1;
1342}
1343
Mark Brown38357ab2009-06-06 19:03:23 +01001344static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1345 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001346 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001347{
Mark Brown828a8422011-01-15 13:14:30 +00001348 int *sort;
1349
1350 if (power_up)
1351 sort = dapm_up_seq;
1352 else
1353 sort = dapm_down_seq;
1354
Mark Brown38357ab2009-06-06 19:03:23 +01001355 if (sort[a->id] != sort[b->id])
1356 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001357 if (a->subseq != b->subseq) {
1358 if (power_up)
1359 return a->subseq - b->subseq;
1360 else
1361 return b->subseq - a->subseq;
1362 }
Mark Brownb22ead22009-06-07 12:51:26 +01001363 if (a->reg != b->reg)
1364 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001365 if (a->dapm != b->dapm)
1366 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001367
Mark Brown38357ab2009-06-06 19:03:23 +01001368 return 0;
1369}
Mark Brown42aa3412009-03-01 19:21:10 +00001370
Mark Brown38357ab2009-06-06 19:03:23 +01001371/* Insert a widget in order into a DAPM power sequence. */
1372static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1373 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001374 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001375{
1376 struct snd_soc_dapm_widget *w;
1377
1378 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001379 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001380 list_add_tail(&new_widget->power_list, &w->power_list);
1381 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001382 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001383
Mark Brown38357ab2009-06-06 19:03:23 +01001384 list_add_tail(&new_widget->power_list, list);
1385}
Mark Brown42aa3412009-03-01 19:21:10 +00001386
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001387static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001388 struct snd_soc_dapm_widget *w, int event)
1389{
Mark Brown68f89ad2010-11-03 23:51:49 -04001390 const char *ev_name;
1391 int power, ret;
1392
1393 switch (event) {
1394 case SND_SOC_DAPM_PRE_PMU:
1395 ev_name = "PRE_PMU";
1396 power = 1;
1397 break;
1398 case SND_SOC_DAPM_POST_PMU:
1399 ev_name = "POST_PMU";
1400 power = 1;
1401 break;
1402 case SND_SOC_DAPM_PRE_PMD:
1403 ev_name = "PRE_PMD";
1404 power = 0;
1405 break;
1406 case SND_SOC_DAPM_POST_PMD:
1407 ev_name = "POST_PMD";
1408 power = 0;
1409 break;
Mark Brown80114122013-02-25 15:14:19 +00001410 case SND_SOC_DAPM_WILL_PMU:
1411 ev_name = "WILL_PMU";
1412 power = 1;
1413 break;
1414 case SND_SOC_DAPM_WILL_PMD:
1415 ev_name = "WILL_PMD";
1416 power = 0;
1417 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001418 default:
1419 BUG();
1420 return;
1421 }
1422
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001423 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001424 return;
1425
1426 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001427 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001429 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001431 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001432 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001433 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001434 ev_name, w->name, ret);
1435 }
1436}
1437
Mark Brownb22ead22009-06-07 12:51:26 +01001438/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001439static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001440 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001441{
Mark Brown68f89ad2010-11-03 23:51:49 -04001442 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001443 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001444 unsigned int value = 0;
1445 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001446
1447 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1448 power_list)->reg;
1449
1450 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001451 BUG_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001452 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001453
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001454 mask |= w->mask << w->shift;
1455 if (w->power)
1456 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001457 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001458 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001459
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001460 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001461 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1462 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001463
Mark Brown68f89ad2010-11-03 23:51:49 -04001464 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001465 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1466 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001467 }
1468
Mark Brown81628102009-06-07 13:21:24 +01001469 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001470 /* Any widget will do, they should all be updating the
1471 * same register.
1472 */
1473 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1474 power_list);
1475
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001476 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001477 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001478 value, mask, reg, card->pop_time);
1479 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001480 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001481 }
1482
1483 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001484 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1485 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001486 }
Mark Brown42aa3412009-03-01 19:21:10 +00001487}
1488
Mark Brownb22ead22009-06-07 12:51:26 +01001489/* Apply a DAPM power sequence.
1490 *
1491 * We walk over a pre-sorted list of widgets to apply power to. In
1492 * order to minimise the number of writes to the device required
1493 * multiple widgets will be updated in a single write where possible.
1494 * Currently anything that requires more than a single write is not
1495 * handled.
1496 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001497static void dapm_seq_run(struct snd_soc_card *card,
1498 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001499{
1500 struct snd_soc_dapm_widget *w, *n;
1501 LIST_HEAD(pending);
1502 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001503 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001504 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001505 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001506 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001507 int *sort;
1508
1509 if (power_up)
1510 sort = dapm_up_seq;
1511 else
1512 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001513
Mark Brownb22ead22009-06-07 12:51:26 +01001514 list_for_each_entry_safe(w, n, list, power_list) {
1515 ret = 0;
1516
1517 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001518 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001519 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001520 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001521 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001522
Mark Brown474b62d2011-01-18 16:14:44 +00001523 if (cur_dapm && cur_dapm->seq_notifier) {
1524 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1525 if (sort[i] == cur_sort)
1526 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001527 i,
1528 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001529 }
1530
Mark Brownb22ead22009-06-07 12:51:26 +01001531 INIT_LIST_HEAD(&pending);
1532 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001533 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001534 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001535 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001536 }
1537
Mark Brown163cac02009-06-07 10:12:52 +01001538 switch (w->id) {
1539 case snd_soc_dapm_pre:
1540 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001541 list_for_each_entry_safe_continue(w, n, list,
1542 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001543
Mark Brownb22ead22009-06-07 12:51:26 +01001544 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001545 ret = w->event(w,
1546 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001547 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001548 ret = w->event(w,
1549 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001550 break;
1551
1552 case snd_soc_dapm_post:
1553 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001554 list_for_each_entry_safe_continue(w, n, list,
1555 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001556
Mark Brownb22ead22009-06-07 12:51:26 +01001557 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001558 ret = w->event(w,
1559 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001560 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001563 break;
1564
Mark Brown163cac02009-06-07 10:12:52 +01001565 default:
Mark Brown81628102009-06-07 13:21:24 +01001566 /* Queue it up for application */
1567 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001568 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001569 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001570 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001571 list_move(&w->power_list, &pending);
1572 break;
Mark Brown163cac02009-06-07 10:12:52 +01001573 }
Mark Brownb22ead22009-06-07 12:51:26 +01001574
1575 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001576 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001577 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001578 }
Mark Brownb22ead22009-06-07 12:51:26 +01001579
1580 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001582
1583 if (cur_dapm && cur_dapm->seq_notifier) {
1584 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1585 if (sort[i] == cur_sort)
1586 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001587 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001588 }
Mark Brown163cac02009-06-07 10:12:52 +01001589}
1590
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001591static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001592{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001593 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001594 struct snd_soc_dapm_widget_list *wlist;
1595 struct snd_soc_dapm_widget *w = NULL;
1596 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001597 int ret;
1598
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001599 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001600 return;
1601
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001602 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001603
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001604 for (wi = 0; wi < wlist->num_widgets; wi++) {
1605 w = wlist->widgets[wi];
1606
1607 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1608 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1609 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001610 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001611 w->name, ret);
1612 }
Mark Brown97404f22010-12-14 16:13:57 +00001613 }
1614
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001615 if (!w)
1616 return;
1617
Liam Girdwood49575fb52012-03-06 18:16:19 +00001618 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001619 update->val);
1620 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001621 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001622 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001623
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001624 for (wi = 0; wi < wlist->num_widgets; wi++) {
1625 w = wlist->widgets[wi];
1626
1627 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1628 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1629 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001630 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001631 w->name, ret);
1632 }
Mark Brown97404f22010-12-14 16:13:57 +00001633 }
1634}
1635
Mark Brown9d0624a2011-02-18 11:49:43 -08001636/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1637 * they're changing state.
1638 */
1639static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1640{
1641 struct snd_soc_dapm_context *d = data;
1642 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001643
Mark Brown56fba412011-06-04 11:25:10 +01001644 /* If we're off and we're not supposed to be go into STANDBY */
1645 if (d->bias_level == SND_SOC_BIAS_OFF &&
1646 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001647 if (d->dev)
1648 pm_runtime_get_sync(d->dev);
1649
Mark Brown9d0624a2011-02-18 11:49:43 -08001650 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1651 if (ret != 0)
1652 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001653 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001654 }
1655
Mark Brown56fba412011-06-04 11:25:10 +01001656 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1657 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001658 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1659 if (ret != 0)
1660 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001661 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001662 }
1663}
1664
1665/* Async callback run prior to DAPM sequences - brings to their final
1666 * state.
1667 */
1668static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1669{
1670 struct snd_soc_dapm_context *d = data;
1671 int ret;
1672
1673 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001674 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1675 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1676 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001677 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1678 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001679 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001680 ret);
1681 }
1682
1683 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001684 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1685 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001686 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1687 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001688 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1689 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001690
1691 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001692 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001693 }
1694
1695 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001696 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1697 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001698 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1699 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001700 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001701 ret);
1702 }
1703}
Mark Brown97404f22010-12-14 16:13:57 +00001704
Mark Brownfe4fda52011-10-03 22:36:57 +01001705static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1706 bool power, bool connect)
1707{
1708 /* If a connection is being made or broken then that update
1709 * will have marked the peer dirty, otherwise the widgets are
1710 * not connected and this update has no impact. */
1711 if (!connect)
1712 return;
1713
1714 /* If the peer is already in the state we're moving to then we
1715 * won't have an impact on it. */
1716 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001717 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001718}
1719
Mark Brown05623c42011-09-28 17:02:31 +01001720static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1721 struct list_head *up_list,
1722 struct list_head *down_list)
1723{
Mark Browndb432b42011-10-03 21:06:40 +01001724 struct snd_soc_dapm_path *path;
1725
Mark Brown05623c42011-09-28 17:02:31 +01001726 if (w->power == power)
1727 return;
1728
1729 trace_snd_soc_dapm_widget_power(w, power);
1730
Mark Browndb432b42011-10-03 21:06:40 +01001731 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001732 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001733 */
1734 list_for_each_entry(path, &w->sources, list_sink) {
1735 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001736 dapm_widget_set_peer_power(path->source, power,
1737 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001738 }
1739 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001740 switch (w->id) {
1741 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001742 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001743 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001744 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001745 /* Supplies can't affect their outputs, only their inputs */
1746 break;
1747 default:
1748 list_for_each_entry(path, &w->sinks, list_source) {
1749 if (path->sink) {
1750 dapm_widget_set_peer_power(path->sink, power,
1751 path->connect);
1752 }
Mark Browndb432b42011-10-03 21:06:40 +01001753 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001754 break;
Mark Browndb432b42011-10-03 21:06:40 +01001755 }
1756
Mark Brown05623c42011-09-28 17:02:31 +01001757 if (power)
1758 dapm_seq_insert(w, up_list, true);
1759 else
1760 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001761}
1762
Mark Brown7c81beb2011-09-20 22:22:32 +01001763static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1764 struct list_head *up_list,
1765 struct list_head *down_list)
1766{
Mark Brown7c81beb2011-09-20 22:22:32 +01001767 int power;
1768
1769 switch (w->id) {
1770 case snd_soc_dapm_pre:
1771 dapm_seq_insert(w, down_list, false);
1772 break;
1773 case snd_soc_dapm_post:
1774 dapm_seq_insert(w, up_list, true);
1775 break;
1776
1777 default:
Mark Brownd8050022011-09-28 18:28:23 +01001778 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001779
Mark Brown05623c42011-09-28 17:02:31 +01001780 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001781 break;
1782 }
1783}
1784
Mark Brown42aa3412009-03-01 19:21:10 +00001785/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786 * Scan each dapm widget for complete audio path.
1787 * A complete path is a route that has valid endpoints i.e.:-
1788 *
1789 * o DAC to output pin.
1790 * o Input Pin to ADC.
1791 * o Input pin to Output pin (bypass, sidetone)
1792 * o DAC to ADC (loopback).
1793 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001794static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001795{
1796 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001797 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001798 LIST_HEAD(up_list);
1799 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001800 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001801 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802
Mark Brown84e90932010-11-04 00:07:02 -04001803 trace_snd_soc_dapm_start(card);
1804
Mark Brown56fba412011-06-04 11:25:10 +01001805 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001806 if (d->idle_bias_off)
1807 d->target_bias_level = SND_SOC_BIAS_OFF;
1808 else
1809 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001810 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001811
Liam Girdwood6c120e12012-02-15 15:15:34 +00001812 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001813
Mark Brown6d3ddc82009-05-16 17:47:29 +01001814 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001815 * lists indicating if they should be powered up or down. We
1816 * only check widgets that have been flagged as dirty but note
1817 * that new widgets may be added to the dirty list while we
1818 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001819 */
Mark Browndb432b42011-10-03 21:06:40 +01001820 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001821 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822 }
1823
Mark Brownf9de6d72011-09-28 17:19:47 +01001824 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001825 switch (w->id) {
1826 case snd_soc_dapm_pre:
1827 case snd_soc_dapm_post:
1828 /* These widgets always need to be powered */
1829 break;
1830 default:
1831 list_del_init(&w->dirty);
1832 break;
1833 }
Mark Browndb432b42011-10-03 21:06:40 +01001834
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001835 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001836 d = w->dapm;
1837
1838 /* Supplies and micbiases only bring the
1839 * context up to STANDBY as unless something
1840 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001841 * generally don't require full power. Signal
1842 * generators are virtual pins and have no
1843 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001844 */
1845 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001846 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001847 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001848 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001849 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001850 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001851 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001852 case snd_soc_dapm_micbias:
1853 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1854 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1855 break;
1856 default:
1857 d->target_bias_level = SND_SOC_BIAS_ON;
1858 break;
1859 }
1860 }
1861
1862 }
1863
Mark Brown85a843c2011-09-21 21:29:47 +01001864 /* Force all contexts in the card to the same bias state if
1865 * they're not ground referenced.
1866 */
Mark Brown56fba412011-06-04 11:25:10 +01001867 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001868 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001869 if (d->target_bias_level > bias)
1870 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001871 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001872 if (!d->idle_bias_off)
1873 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001874
Mark Brownde02d072011-09-20 21:43:24 +01001875 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001876
Mark Brown9d0624a2011-02-18 11:49:43 -08001877 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001878 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001879 async_schedule_domain(dapm_pre_sequence_async, d,
1880 &async_domain);
1881 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001882
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001883 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001884 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001885 }
1886
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001887 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001888 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001889 }
1890
Mark Brown6d3ddc82009-05-16 17:47:29 +01001891 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001892 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001893
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001894 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001895
Mark Brown6d3ddc82009-05-16 17:47:29 +01001896 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001897 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001898
Mark Brown9d0624a2011-02-18 11:49:43 -08001899 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001900 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001901 async_schedule_domain(dapm_post_sequence_async, d,
1902 &async_domain);
1903 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001904
Liam Girdwood8078d872012-02-15 15:15:35 +00001905 /* do we need to notify any clients that DAPM event is complete */
1906 list_for_each_entry(d, &card->dapm_list, list) {
1907 if (d->stream_event)
1908 d->stream_event(d, event);
1909 }
1910
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001911 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001912 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001913 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001914
Mark Brown84e90932010-11-04 00:07:02 -04001915 trace_snd_soc_dapm_done(card);
1916
Mark Brown42aa3412009-03-01 19:21:10 +00001917 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001918}
1919
Mark Brown79fb9382009-08-21 16:38:13 +01001920#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001921static ssize_t dapm_widget_power_read_file(struct file *file,
1922 char __user *user_buf,
1923 size_t count, loff_t *ppos)
1924{
1925 struct snd_soc_dapm_widget *w = file->private_data;
1926 char *buf;
1927 int in, out;
1928 ssize_t ret;
1929 struct snd_soc_dapm_path *p = NULL;
1930
1931 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1932 if (!buf)
1933 return -ENOMEM;
1934
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001935 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001936 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001937 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001938 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001939
Mark Brownf13ebad2012-03-03 18:01:01 +00001940 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1941 w->name, w->power ? "On" : "Off",
1942 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001943
Mark Brownd033c362009-12-04 15:25:56 +00001944 if (w->reg >= 0)
1945 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001946 " - R%d(0x%x) mask 0x%x",
1947 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001948
1949 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1950
Mark Brown3eef08b2009-09-14 16:49:00 +01001951 if (w->sname)
1952 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1953 w->sname,
1954 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001955
1956 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001957 if (p->connected && !p->connected(w, p->sink))
1958 continue;
1959
Mark Brown79fb9382009-08-21 16:38:13 +01001960 if (p->connect)
1961 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001962 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001963 p->name ? p->name : "static",
1964 p->source->name);
1965 }
1966 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001967 if (p->connected && !p->connected(w, p->sink))
1968 continue;
1969
Mark Brown79fb9382009-08-21 16:38:13 +01001970 if (p->connect)
1971 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001972 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001973 p->name ? p->name : "static",
1974 p->sink->name);
1975 }
1976
1977 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1978
1979 kfree(buf);
1980 return ret;
1981}
1982
1983static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001984 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001985 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001986 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001987};
1988
Mark Brownef49e4f2011-04-04 20:48:13 +09001989static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1990 size_t count, loff_t *ppos)
1991{
1992 struct snd_soc_dapm_context *dapm = file->private_data;
1993 char *level;
1994
1995 switch (dapm->bias_level) {
1996 case SND_SOC_BIAS_ON:
1997 level = "On\n";
1998 break;
1999 case SND_SOC_BIAS_PREPARE:
2000 level = "Prepare\n";
2001 break;
2002 case SND_SOC_BIAS_STANDBY:
2003 level = "Standby\n";
2004 break;
2005 case SND_SOC_BIAS_OFF:
2006 level = "Off\n";
2007 break;
2008 default:
2009 BUG();
2010 level = "Unknown\n";
2011 break;
2012 }
2013
2014 return simple_read_from_buffer(user_buf, count, ppos, level,
2015 strlen(level));
2016}
2017
2018static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002019 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002020 .read = dapm_bias_read_file,
2021 .llseek = default_llseek,
2022};
2023
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002024void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2025 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002026{
Mark Brown79fb9382009-08-21 16:38:13 +01002027 struct dentry *d;
2028
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002029 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2030
2031 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002032 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002033 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002034 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002035 }
Mark Brown79fb9382009-08-21 16:38:13 +01002036
Mark Brownef49e4f2011-04-04 20:48:13 +09002037 d = debugfs_create_file("bias_level", 0444,
2038 dapm->debugfs_dapm, dapm,
2039 &dapm_bias_fops);
2040 if (!d)
2041 dev_warn(dapm->dev,
2042 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002043}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002044
2045static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2046{
2047 struct snd_soc_dapm_context *dapm = w->dapm;
2048 struct dentry *d;
2049
2050 if (!dapm->debugfs_dapm || !w->name)
2051 return;
2052
2053 d = debugfs_create_file(w->name, 0444,
2054 dapm->debugfs_dapm, w,
2055 &dapm_widget_power_fops);
2056 if (!d)
2057 dev_warn(w->dapm->dev,
2058 "ASoC: Failed to create %s debugfs file\n",
2059 w->name);
2060}
2061
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002062static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2063{
2064 debugfs_remove_recursive(dapm->debugfs_dapm);
2065}
2066
Mark Brown79fb9382009-08-21 16:38:13 +01002067#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002068void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2069 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002070{
2071}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002072
2073static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2074{
2075}
2076
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002077static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2078{
2079}
2080
Mark Brown79fb9382009-08-21 16:38:13 +01002081#endif
2082
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002084static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002085 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002086{
2087 struct snd_soc_dapm_path *path;
2088 int found = 0;
2089
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002091 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002092 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 continue;
2094
2095 found = 1;
2096 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002097 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002099 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002100 } else {
2101 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002102 dapm_mark_dirty(path->source,
2103 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002105 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002106 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 }
2108
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002109 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002110 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111
Liam Girdwood618dae12012-04-25 12:12:51 +01002112 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002114
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002115int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002116 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2117 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002118{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002119 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002120 int ret;
2121
Liam Girdwood3cd04342012-03-09 12:02:08 +00002122 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002123 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002124 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002125 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002126 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002127 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002128 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002129 return ret;
2130}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002131EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132
Milan plzik1b075e32008-01-10 14:39:46 +01002133/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002134static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002135 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136{
2137 struct snd_soc_dapm_path *path;
2138 int found = 0;
2139
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002141 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002143 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002144 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002145 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146 }
2147
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002148 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002149 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150
Liam Girdwood618dae12012-04-25 12:12:51 +01002151 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002153
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002154int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002155 struct snd_kcontrol *kcontrol, int connect,
2156 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002157{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002158 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002159 int ret;
2160
Liam Girdwood3cd04342012-03-09 12:02:08 +00002161 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002162 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002163 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002164 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002165 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002166 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002167 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002168 return ret;
2169}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002170EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171
2172/* show dapm widget status in sys fs */
2173static ssize_t dapm_widget_show(struct device *dev,
2174 struct device_attribute *attr, char *buf)
2175{
Mark Brown36ae1a92012-01-06 17:12:45 -08002176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002177 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 struct snd_soc_dapm_widget *w;
2179 int count = 0;
2180 char *state = "not set";
2181
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002182 list_for_each_entry(w, &codec->card->widgets, list) {
2183 if (w->dapm != &codec->dapm)
2184 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185
2186 /* only display widgets that burnm power */
2187 switch (w->id) {
2188 case snd_soc_dapm_hp:
2189 case snd_soc_dapm_mic:
2190 case snd_soc_dapm_spk:
2191 case snd_soc_dapm_line:
2192 case snd_soc_dapm_micbias:
2193 case snd_soc_dapm_dac:
2194 case snd_soc_dapm_adc:
2195 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002196 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002198 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002199 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002200 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002201 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202 if (w->name)
2203 count += sprintf(buf + count, "%s: %s\n",
2204 w->name, w->power ? "On":"Off");
2205 break;
2206 default:
2207 break;
2208 }
2209 }
2210
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002211 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002212 case SND_SOC_BIAS_ON:
2213 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 break;
Mark Brown0be98982008-05-19 12:31:28 +02002215 case SND_SOC_BIAS_PREPARE:
2216 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 break;
Mark Brown0be98982008-05-19 12:31:28 +02002218 case SND_SOC_BIAS_STANDBY:
2219 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 break;
Mark Brown0be98982008-05-19 12:31:28 +02002221 case SND_SOC_BIAS_OFF:
2222 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223 break;
2224 }
2225 count += sprintf(buf + count, "PM State: %s\n", state);
2226
2227 return count;
2228}
2229
2230static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2231
2232int snd_soc_dapm_sys_add(struct device *dev)
2233{
Troy Kisky12ef1932008-10-13 17:42:14 -07002234 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235}
2236
2237static void snd_soc_dapm_sys_remove(struct device *dev)
2238{
Mark Brownaef90842009-05-16 17:53:16 +01002239 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240}
2241
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002242static void dapm_free_path(struct snd_soc_dapm_path *path)
2243{
2244 list_del(&path->list_sink);
2245 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002246 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002247 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002248 kfree(path);
2249}
2250
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002252static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002253{
2254 struct snd_soc_dapm_widget *w, *next_w;
2255 struct snd_soc_dapm_path *p, *next_p;
2256
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002257 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2258 if (w->dapm != dapm)
2259 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002261 /*
2262 * remove source and sink paths associated to this widget.
2263 * While removing the path, remove reference to it from both
2264 * source and sink widgets so that path is removed only once.
2265 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002266 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2267 dapm_free_path(p);
2268
2269 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2270 dapm_free_path(p);
2271
Stephen Warrenfad59882011-04-28 17:37:59 -06002272 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002273 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 kfree(w);
2275 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276}
2277
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002278static struct snd_soc_dapm_widget *dapm_find_widget(
2279 struct snd_soc_dapm_context *dapm, const char *pin,
2280 bool search_other_contexts)
2281{
2282 struct snd_soc_dapm_widget *w;
2283 struct snd_soc_dapm_widget *fallback = NULL;
2284
2285 list_for_each_entry(w, &dapm->card->widgets, list) {
2286 if (!strcmp(w->name, pin)) {
2287 if (w->dapm == dapm)
2288 return w;
2289 else
2290 fallback = w;
2291 }
2292 }
2293
2294 if (search_other_contexts)
2295 return fallback;
2296
2297 return NULL;
2298}
2299
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002300static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002301 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002302{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002303 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002304
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002305 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002306 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002307 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002308 }
2309
Mark Brown1a8b2d92012-02-16 11:50:07 -08002310 if (w->connected != status)
2311 dapm_mark_dirty(w, "pin configuration");
2312
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002313 w->connected = status;
2314 if (status == 0)
2315 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002316
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002317 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002318}
2319
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002321 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002322 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 *
2324 * Walks all dapm audio paths and powers widgets according to their
2325 * stream or path usage.
2326 *
2327 * Returns 0 for success.
2328 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002329int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002331 int ret;
2332
Mark Brown4f4c0072011-10-07 14:29:19 +01002333 /*
2334 * Suppress early reports (eg, jacks syncing their state) to avoid
2335 * silly DAPM runs during card startup.
2336 */
2337 if (!dapm->card || !dapm->card->instantiated)
2338 return 0;
2339
Liam Girdwood3cd04342012-03-09 12:02:08 +00002340 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002341 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002342 mutex_unlock(&dapm->card->dapm_mutex);
2343 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344}
Liam Girdwooda5302182008-07-07 13:35:17 +01002345EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002347static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2348 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2349 const char *control,
2350 int (*connected)(struct snd_soc_dapm_widget *source,
2351 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352{
2353 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002354 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355
2356 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2357 if (!path)
2358 return -ENOMEM;
2359
2360 path->source = wsource;
2361 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002362 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002364 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 INIT_LIST_HEAD(&path->list_source);
2366 INIT_LIST_HEAD(&path->list_sink);
2367
2368 /* check for external widgets */
2369 if (wsink->id == snd_soc_dapm_input) {
2370 if (wsource->id == snd_soc_dapm_micbias ||
2371 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002372 wsource->id == snd_soc_dapm_line ||
2373 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 wsink->ext = 1;
2375 }
2376 if (wsource->id == snd_soc_dapm_output) {
2377 if (wsink->id == snd_soc_dapm_spk ||
2378 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002379 wsink->id == snd_soc_dapm_line ||
2380 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002381 wsource->ext = 1;
2382 }
2383
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002384 dapm_mark_dirty(wsource, "Route added");
2385 dapm_mark_dirty(wsink, "Route added");
2386
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 /* connect static paths */
2388 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002389 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390 list_add(&path->list_sink, &wsink->sources);
2391 list_add(&path->list_source, &wsource->sinks);
2392 path->connect = 1;
2393 return 0;
2394 }
2395
2396 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002397 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002398 case snd_soc_dapm_adc:
2399 case snd_soc_dapm_dac:
2400 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002401 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402 case snd_soc_dapm_input:
2403 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002404 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002405 case snd_soc_dapm_micbias:
2406 case snd_soc_dapm_vmid:
2407 case snd_soc_dapm_pre:
2408 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002409 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002410 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002411 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002412 case snd_soc_dapm_aif_in:
2413 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002414 case snd_soc_dapm_dai_in:
2415 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002416 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002417 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002418 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 list_add(&path->list_sink, &wsink->sources);
2420 list_add(&path->list_source, &wsource->sinks);
2421 path->connect = 1;
2422 return 0;
2423 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002424 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002425 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002426 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002427 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 if (ret != 0)
2429 goto err;
2430 break;
2431 case snd_soc_dapm_switch:
2432 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002433 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002434 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002435 if (ret != 0)
2436 goto err;
2437 break;
2438 case snd_soc_dapm_hp:
2439 case snd_soc_dapm_mic:
2440 case snd_soc_dapm_line:
2441 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002442 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443 list_add(&path->list_sink, &wsink->sources);
2444 list_add(&path->list_source, &wsource->sinks);
2445 path->connect = 0;
2446 return 0;
2447 }
Mark Brownfabd0382012-07-05 17:20:06 +01002448
Richard Purdie2b97eab2006-10-06 18:32:18 +02002449 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002450err:
2451 kfree(path);
2452 return ret;
2453}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002454
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002455static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2456 const struct snd_soc_dapm_route *route)
2457{
2458 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2459 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2460 const char *sink;
2461 const char *source;
2462 char prefixed_sink[80];
2463 char prefixed_source[80];
2464 int ret;
2465
2466 if (dapm->codec && dapm->codec->name_prefix) {
2467 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2468 dapm->codec->name_prefix, route->sink);
2469 sink = prefixed_sink;
2470 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2471 dapm->codec->name_prefix, route->source);
2472 source = prefixed_source;
2473 } else {
2474 sink = route->sink;
2475 source = route->source;
2476 }
2477
2478 /*
2479 * find src and dest widgets over all widgets but favor a widget from
2480 * current DAPM context
2481 */
2482 list_for_each_entry(w, &dapm->card->widgets, list) {
2483 if (!wsink && !(strcmp(w->name, sink))) {
2484 wtsink = w;
2485 if (w->dapm == dapm)
2486 wsink = w;
2487 continue;
2488 }
2489 if (!wsource && !(strcmp(w->name, source))) {
2490 wtsource = w;
2491 if (w->dapm == dapm)
2492 wsource = w;
2493 }
2494 }
2495 /* use widget from another DAPM context if not found from this */
2496 if (!wsink)
2497 wsink = wtsink;
2498 if (!wsource)
2499 wsource = wtsource;
2500
2501 if (wsource == NULL) {
2502 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2503 route->source);
2504 return -ENODEV;
2505 }
2506 if (wsink == NULL) {
2507 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2508 route->sink);
2509 return -ENODEV;
2510 }
2511
2512 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2513 route->connected);
2514 if (ret)
2515 goto err;
2516
2517 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002518err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002519 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002520 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002521 return ret;
2522}
Mark Brown105f1c22008-05-13 14:52:19 +02002523
Mark Brownefcc3c62012-07-05 17:24:19 +01002524static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2525 const struct snd_soc_dapm_route *route)
2526{
2527 struct snd_soc_dapm_path *path, *p;
2528 const char *sink;
2529 const char *source;
2530 char prefixed_sink[80];
2531 char prefixed_source[80];
2532
2533 if (route->control) {
2534 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002535 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002536 return -EINVAL;
2537 }
2538
2539 if (dapm->codec && dapm->codec->name_prefix) {
2540 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2541 dapm->codec->name_prefix, route->sink);
2542 sink = prefixed_sink;
2543 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2544 dapm->codec->name_prefix, route->source);
2545 source = prefixed_source;
2546 } else {
2547 sink = route->sink;
2548 source = route->source;
2549 }
2550
2551 path = NULL;
2552 list_for_each_entry(p, &dapm->card->paths, list) {
2553 if (strcmp(p->source->name, source) != 0)
2554 continue;
2555 if (strcmp(p->sink->name, sink) != 0)
2556 continue;
2557 path = p;
2558 break;
2559 }
2560
2561 if (path) {
2562 dapm_mark_dirty(path->source, "Route removed");
2563 dapm_mark_dirty(path->sink, "Route removed");
2564
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002565 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002566 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002567 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002568 source, sink);
2569 }
2570
2571 return 0;
2572}
2573
Mark Brown105f1c22008-05-13 14:52:19 +02002574/**
Mark Brown105f1c22008-05-13 14:52:19 +02002575 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002576 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002577 * @route: audio routes
2578 * @num: number of routes
2579 *
2580 * Connects 2 dapm widgets together via a named audio path. The sink is
2581 * the widget receiving the audio signal, whilst the source is the sender
2582 * of the audio signal.
2583 *
2584 * Returns 0 for success else error. On error all resources can be freed
2585 * with a call to snd_soc_card_free().
2586 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002587int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002588 const struct snd_soc_dapm_route *route, int num)
2589{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002590 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002591
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002592 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002593 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002594 r = snd_soc_dapm_add_route(dapm, route);
2595 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002596 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2597 route->source,
2598 route->control ? route->control : "direct",
2599 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002600 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002601 }
2602 route++;
2603 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002604 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002605
Dan Carpenter60884c22012-04-13 22:25:43 +03002606 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002607}
2608EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2609
Mark Brownefcc3c62012-07-05 17:24:19 +01002610/**
2611 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2612 * @dapm: DAPM context
2613 * @route: audio routes
2614 * @num: number of routes
2615 *
2616 * Removes routes from the DAPM context.
2617 */
2618int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2619 const struct snd_soc_dapm_route *route, int num)
2620{
2621 int i, ret = 0;
2622
2623 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2624 for (i = 0; i < num; i++) {
2625 snd_soc_dapm_del_route(dapm, route);
2626 route++;
2627 }
2628 mutex_unlock(&dapm->card->dapm_mutex);
2629
2630 return ret;
2631}
2632EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2633
Mark Brownbf3a9e12011-06-13 16:42:29 +01002634static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2635 const struct snd_soc_dapm_route *route)
2636{
2637 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2638 route->source,
2639 true);
2640 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2641 route->sink,
2642 true);
2643 struct snd_soc_dapm_path *path;
2644 int count = 0;
2645
2646 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002647 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002648 route->source);
2649 return -ENODEV;
2650 }
2651
2652 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002653 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002654 route->sink);
2655 return -ENODEV;
2656 }
2657
2658 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002659 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002660 route->source, route->sink);
2661
2662 list_for_each_entry(path, &source->sinks, list_source) {
2663 if (path->sink == sink) {
2664 path->weak = 1;
2665 count++;
2666 }
2667 }
2668
2669 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002670 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002671 route->source, route->sink);
2672 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002673 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002674 count, route->source, route->sink);
2675
2676 return 0;
2677}
2678
2679/**
2680 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2681 * @dapm: DAPM context
2682 * @route: audio routes
2683 * @num: number of routes
2684 *
2685 * Mark existing routes matching those specified in the passed array
2686 * as being weak, meaning that they are ignored for the purpose of
2687 * power decisions. The main intended use case is for sidetone paths
2688 * which couple audio between other independent paths if they are both
2689 * active in order to make the combination work better at the user
2690 * level but which aren't intended to be "used".
2691 *
2692 * Note that CODEC drivers should not use this as sidetone type paths
2693 * can frequently also be used as bypass paths.
2694 */
2695int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2696 const struct snd_soc_dapm_route *route, int num)
2697{
2698 int i, err;
2699 int ret = 0;
2700
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002701 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002702 for (i = 0; i < num; i++) {
2703 err = snd_soc_dapm_weak_route(dapm, route);
2704 if (err)
2705 ret = err;
2706 route++;
2707 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002708 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002709
2710 return ret;
2711}
2712EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2713
Mark Brown105f1c22008-05-13 14:52:19 +02002714/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002716 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 *
2718 * Checks the codec for any new dapm widgets and creates them if found.
2719 *
2720 * Returns 0 for success.
2721 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002722int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002723{
2724 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002725 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002727 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002728
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002729 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002730 {
2731 if (w->new)
2732 continue;
2733
Stephen Warrenfad59882011-04-28 17:37:59 -06002734 if (w->num_kcontrols) {
2735 w->kcontrols = kzalloc(w->num_kcontrols *
2736 sizeof(struct snd_kcontrol *),
2737 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002738 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002739 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002740 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002741 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002742 }
2743
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 switch(w->id) {
2745 case snd_soc_dapm_switch:
2746 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002747 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002748 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 break;
2750 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002751 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002752 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002753 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002756 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002757 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002759 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760 break;
2761 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002762
2763 /* Read the initial power state from the device */
2764 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002765 val = soc_widget_read(w, w->reg) >> w->shift;
2766 val &= w->mask;
2767 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002768 w->power = 1;
2769 }
2770
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002772
Mark Brown7508b122011-10-05 12:09:12 +01002773 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002774 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 }
2776
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002777 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2778 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779 return 0;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2782
2783/**
2784 * snd_soc_dapm_get_volsw - dapm mixer get callback
2785 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002786 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787 *
2788 * Callback to get the value of a dapm mixer control.
2789 *
2790 * Returns 0 for success.
2791 */
2792int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002795 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002796 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002797 struct soc_mixer_control *mc =
2798 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002799 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002800 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002801 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002802 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002803 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002804 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002805
2806 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002807 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002808 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002809 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002810
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002811 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002812 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002813 val = (snd_soc_read(codec, reg) >> shift) & mask;
2814 else
2815 val = dapm_kcontrol_get_value(kcontrol);
2816 mutex_unlock(&card->dapm_mutex);
2817
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002818 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002819 ucontrol->value.integer.value[0] = max - val;
2820 else
2821 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822
2823 return 0;
2824}
2825EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2826
2827/**
2828 * snd_soc_dapm_put_volsw - dapm mixer set callback
2829 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002830 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002831 *
2832 * Callback to set the value of a dapm mixer control.
2833 *
2834 * Returns 0 for success.
2835 */
2836int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2838{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002839 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002840 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 struct soc_mixer_control *mc =
2842 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002843 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002844 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002845 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002846 unsigned int mask = (1 << fls(max)) - 1;
2847 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002848 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002849 int connect, change;
2850 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002852 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002853 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002854 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002855 kcontrol->id.name);
2856
Richard Purdie2b97eab2006-10-06 18:32:18 +02002857 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002858 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002859
2860 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002861 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862
Liam Girdwood3cd04342012-03-09 12:02:08 +00002863 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002864
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002865 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002866
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002867 if (reg != SND_SOC_NOPM) {
2868 mask = mask << shift;
2869 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002870
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002871 change = snd_soc_test_bits(codec, reg, mask, val);
2872 }
2873
Mark Brown97404f22010-12-14 16:13:57 +00002874 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002875 if (reg != SND_SOC_NOPM) {
2876 update.kcontrol = kcontrol;
2877 update.reg = reg;
2878 update.mask = mask;
2879 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002880
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002881 card->update = &update;
2882 }
Mark Brown97404f22010-12-14 16:13:57 +00002883
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002884 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002885
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002886 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002887 }
2888
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002889 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002890 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002891}
2892EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2893
2894/**
2895 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2896 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002897 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002898 *
2899 * Callback to get the value of a dapm enumerated double mixer control.
2900 *
2901 * Returns 0 for success.
2902 */
2903int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
2905{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002906 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002907 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002908 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002910 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002911 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002912 if (e->shift_l != e->shift_r)
2913 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002914 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002915
2916 return 0;
2917}
2918EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2919
2920/**
2921 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2922 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002923 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924 *
2925 * Callback to set the value of a dapm enumerated double mixer control.
2926 *
2927 * Returns 0 for success.
2928 */
2929int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2931{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002932 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002933 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002934 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002935 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002936 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002937 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002938
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002939 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002940 return -EINVAL;
2941 mux = ucontrol->value.enumerated.item[0];
2942 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002943 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002944 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002945 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002946 return -EINVAL;
2947 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002948 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002949 }
2950
Liam Girdwood3cd04342012-03-09 12:02:08 +00002951 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002952
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002953 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002954 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002955 update.kcontrol = kcontrol;
2956 update.reg = e->reg;
2957 update.mask = mask;
2958 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002959 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002960
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002961 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002962
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002963 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002964 }
2965
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002966 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002967 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002968}
2969EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2970
2971/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002972 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2973 * @kcontrol: mixer control
2974 * @ucontrol: control element information
2975 *
2976 * Returns 0 for success.
2977 */
2978int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2979 struct snd_ctl_elem_value *ucontrol)
2980{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002981 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002982 return 0;
2983}
2984EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2985
2986/**
2987 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2988 * @kcontrol: mixer control
2989 * @ucontrol: control element information
2990 *
2991 * Returns 0 for success.
2992 */
2993int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2995{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002996 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002997 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002998 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002999 struct soc_enum *e =
3000 (struct soc_enum *)kcontrol->private_value;
3001 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003002
3003 if (ucontrol->value.enumerated.item[0] >= e->max)
3004 return -EINVAL;
3005
Liam Girdwood3cd04342012-03-09 12:02:08 +00003006 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003007
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003008 value = ucontrol->value.enumerated.item[0];
3009 change = dapm_kcontrol_set_value(kcontrol, value);
3010 if (change)
3011 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003012
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003013 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003014 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003015}
3016EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3017
3018/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003019 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3020 * callback
3021 * @kcontrol: mixer control
3022 * @ucontrol: control element information
3023 *
3024 * Callback to get the value of a dapm semi enumerated double mixer control.
3025 *
3026 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3027 * used for handling bitfield coded enumeration for example.
3028 *
3029 * Returns 0 for success.
3030 */
3031int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_value *ucontrol)
3033{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003034 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003035 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003036 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003037
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003038 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003039 val = (reg_val >> e->shift_l) & e->mask;
3040 for (mux = 0; mux < e->max; mux++) {
3041 if (val == e->values[mux])
3042 break;
3043 }
3044 ucontrol->value.enumerated.item[0] = mux;
3045 if (e->shift_l != e->shift_r) {
3046 val = (reg_val >> e->shift_r) & e->mask;
3047 for (mux = 0; mux < e->max; mux++) {
3048 if (val == e->values[mux])
3049 break;
3050 }
3051 ucontrol->value.enumerated.item[1] = mux;
3052 }
3053
3054 return 0;
3055}
3056EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3057
3058/**
3059 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3060 * callback
3061 * @kcontrol: mixer control
3062 * @ucontrol: control element information
3063 *
3064 * Callback to set 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_put_value_enum_double(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol)
3073{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003074 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003075 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003076 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003077 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003078 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003079 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003080
3081 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3082 return -EINVAL;
3083 mux = ucontrol->value.enumerated.item[0];
3084 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3085 mask = e->mask << e->shift_l;
3086 if (e->shift_l != e->shift_r) {
3087 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3088 return -EINVAL;
3089 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3090 mask |= e->mask << e->shift_r;
3091 }
3092
Liam Girdwood3cd04342012-03-09 12:02:08 +00003093 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003094
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003095 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003096 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003097 update.kcontrol = kcontrol;
3098 update.reg = e->reg;
3099 update.mask = mask;
3100 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003101 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003102
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003103 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003104
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003105 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003106 }
3107
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003108 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003109 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003110}
3111EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3112
3113/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003114 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3115 *
3116 * @kcontrol: mixer control
3117 * @uinfo: control element information
3118 *
3119 * Callback to provide information about a pin switch control.
3120 */
3121int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_info *uinfo)
3123{
3124 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3125 uinfo->count = 1;
3126 uinfo->value.integer.min = 0;
3127 uinfo->value.integer.max = 1;
3128
3129 return 0;
3130}
3131EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3132
3133/**
3134 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3135 *
3136 * @kcontrol: mixer control
3137 * @ucontrol: Value
3138 */
3139int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3141{
Mark Brown48a8c392012-02-14 17:11:15 -08003142 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003143 const char *pin = (const char *)kcontrol->private_value;
3144
Liam Girdwood3cd04342012-03-09 12:02:08 +00003145 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003146
3147 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003148 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003149
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003150 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003151
3152 return 0;
3153}
3154EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3155
3156/**
3157 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3158 *
3159 * @kcontrol: mixer control
3160 * @ucontrol: Value
3161 */
3162int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_value *ucontrol)
3164{
Mark Brown48a8c392012-02-14 17:11:15 -08003165 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003166 const char *pin = (const char *)kcontrol->private_value;
3167
Liam Girdwood3cd04342012-03-09 12:02:08 +00003168 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003169
3170 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003171 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003172 else
Mark Brown48a8c392012-02-14 17:11:15 -08003173 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003174
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003175 mutex_unlock(&card->dapm_mutex);
3176
Mark Brown48a8c392012-02-14 17:11:15 -08003177 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003178 return 0;
3179}
3180EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3181
Mark Brown5ba06fc2012-02-16 11:07:13 -08003182static struct snd_soc_dapm_widget *
3183snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3184 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003185{
3186 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003187 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003188
3189 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003190 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003191
Mark Brown62ea8742012-01-21 21:14:48 +00003192 switch (w->id) {
3193 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003194 w->regulator = devm_regulator_get(dapm->dev, w->name);
3195 if (IS_ERR(w->regulator)) {
3196 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003197 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003198 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003199 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003200 }
Mark Brown8784c772013-01-10 19:33:47 +00003201
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003202 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003203 ret = regulator_allow_bypass(w->regulator, true);
3204 if (ret != 0)
3205 dev_warn(w->dapm->dev,
3206 "ASoC: Failed to unbypass %s: %d\n",
3207 w->name, ret);
3208 }
Mark Brown62ea8742012-01-21 21:14:48 +00003209 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003210 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003211#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003212 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003213 if (IS_ERR(w->clk)) {
3214 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003215 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003216 w->name, ret);
3217 return NULL;
3218 }
Mark Brownec029952012-06-04 08:16:20 +01003219#else
3220 return NULL;
3221#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003222 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003223 default:
3224 break;
3225 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003226
Mark Brown88e8b9a2011-03-02 18:18:24 +00003227 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003228 w->name = kasprintf(GFP_KERNEL, "%s %s",
3229 dapm->codec->name_prefix, widget->name);
3230 else
3231 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3232
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003233 if (w->name == NULL) {
3234 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003235 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003236 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003237
Mark Brown7ca3a182011-10-08 14:04:50 +01003238 switch (w->id) {
3239 case snd_soc_dapm_switch:
3240 case snd_soc_dapm_mixer:
3241 case snd_soc_dapm_mixer_named_ctl:
3242 w->power_check = dapm_generic_check_power;
3243 break;
3244 case snd_soc_dapm_mux:
3245 case snd_soc_dapm_virt_mux:
3246 case snd_soc_dapm_value_mux:
3247 w->power_check = dapm_generic_check_power;
3248 break;
Mark Brown46162742013-06-05 19:36:11 +01003249 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003250 w->power_check = dapm_adc_check_power;
3251 break;
Mark Brown46162742013-06-05 19:36:11 +01003252 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003253 w->power_check = dapm_dac_check_power;
3254 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003255 case snd_soc_dapm_adc:
3256 case snd_soc_dapm_aif_out:
3257 case snd_soc_dapm_dac:
3258 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003259 case snd_soc_dapm_pga:
3260 case snd_soc_dapm_out_drv:
3261 case snd_soc_dapm_input:
3262 case snd_soc_dapm_output:
3263 case snd_soc_dapm_micbias:
3264 case snd_soc_dapm_spk:
3265 case snd_soc_dapm_hp:
3266 case snd_soc_dapm_mic:
3267 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003268 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003269 w->power_check = dapm_generic_check_power;
3270 break;
3271 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003272 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003273 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003274 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003275 w->power_check = dapm_supply_check_power;
3276 break;
3277 default:
3278 w->power_check = dapm_always_on_check_power;
3279 break;
3280 }
3281
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003282 w->dapm = dapm;
3283 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003284 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003285 INIT_LIST_HEAD(&w->sources);
3286 INIT_LIST_HEAD(&w->sinks);
3287 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003288 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003289 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003290
3291 /* machine layer set ups unconnected pins and insertions */
3292 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003293 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003294}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003295
3296/**
Mark Brown4ba13272008-05-13 14:51:19 +02003297 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003298 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003299 * @widget: widget array
3300 * @num: number of widgets
3301 *
3302 * Creates new DAPM controls based upon the templates.
3303 *
3304 * Returns 0 for success else error.
3305 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003306int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003307 const struct snd_soc_dapm_widget *widget,
3308 int num)
3309{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003310 struct snd_soc_dapm_widget *w;
3311 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003312 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003313
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003314 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003315 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003316 w = snd_soc_dapm_new_control(dapm, widget);
3317 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003318 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003319 "ASoC: Failed to create DAPM control %s\n",
3320 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003321 ret = -ENOMEM;
3322 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003323 }
Mark Brown4ba13272008-05-13 14:51:19 +02003324 widget++;
3325 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003326 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003327 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003328}
3329EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3330
Mark Brownc74184e2012-04-04 22:12:09 +01003331static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3332 struct snd_kcontrol *kcontrol, int event)
3333{
3334 struct snd_soc_dapm_path *source_p, *sink_p;
3335 struct snd_soc_dai *source, *sink;
3336 const struct snd_soc_pcm_stream *config = w->params;
3337 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003338 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003339 u64 fmt;
3340 int ret;
3341
3342 BUG_ON(!config);
3343 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3344
3345 /* We only support a single source and sink, pick the first */
3346 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3347 list_sink);
3348 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3349 list_source);
3350
3351 BUG_ON(!source_p || !sink_p);
3352 BUG_ON(!sink_p->source || !source_p->sink);
3353 BUG_ON(!source_p->source || !sink_p->sink);
3354
3355 source = source_p->source->priv;
3356 sink = sink_p->sink->priv;
3357
3358 /* Be a little careful as we don't want to overflow the mask array */
3359 if (config->formats) {
3360 fmt = ffs(config->formats) - 1;
3361 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003362 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003363 config->formats);
3364 fmt = 0;
3365 }
3366
3367 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003368 params = kzalloc(sizeof(*params), GFP_KERNEL);
3369 if (!params) {
3370 ret = -ENOMEM;
3371 goto out;
3372 }
3373 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003374
Mark Brown9747cec2012-04-26 19:12:21 +01003375 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003376 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003377 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003378 config->rate_max;
3379
Mark Brown9747cec2012-04-26 19:12:21 +01003380 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003381 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003382 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003383 = config->channels_max;
3384
3385 memset(&substream, 0, sizeof(substream));
3386
3387 switch (event) {
3388 case SND_SOC_DAPM_PRE_PMU:
3389 if (source->driver->ops && source->driver->ops->hw_params) {
3390 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3391 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003392 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003393 if (ret != 0) {
3394 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003395 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003396 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003397 }
3398 }
3399
3400 if (sink->driver->ops && sink->driver->ops->hw_params) {
3401 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003402 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003403 sink);
3404 if (ret != 0) {
3405 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003406 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003407 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003408 }
3409 }
3410 break;
3411
3412 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003413 ret = snd_soc_dai_digital_mute(sink, 0,
3414 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003415 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003416 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003417 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003418 break;
3419
3420 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003421 ret = snd_soc_dai_digital_mute(sink, 1,
3422 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003423 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003424 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003425 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003426 break;
3427
3428 default:
3429 BUG();
3430 return -EINVAL;
3431 }
3432
Mark Brown9747cec2012-04-26 19:12:21 +01003433out:
3434 kfree(params);
3435 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003436}
3437
3438int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3439 const struct snd_soc_pcm_stream *params,
3440 struct snd_soc_dapm_widget *source,
3441 struct snd_soc_dapm_widget *sink)
3442{
3443 struct snd_soc_dapm_route routes[2];
3444 struct snd_soc_dapm_widget template;
3445 struct snd_soc_dapm_widget *w;
3446 size_t len;
3447 char *link_name;
3448
3449 len = strlen(source->name) + strlen(sink->name) + 2;
3450 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3451 if (!link_name)
3452 return -ENOMEM;
3453 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3454
3455 memset(&template, 0, sizeof(template));
3456 template.reg = SND_SOC_NOPM;
3457 template.id = snd_soc_dapm_dai_link;
3458 template.name = link_name;
3459 template.event = snd_soc_dai_link_event;
3460 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3461 SND_SOC_DAPM_PRE_PMD;
3462
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003463 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003464
3465 w = snd_soc_dapm_new_control(&card->dapm, &template);
3466 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003467 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003468 link_name);
3469 return -ENOMEM;
3470 }
3471
3472 w->params = params;
3473
3474 memset(&routes, 0, sizeof(routes));
3475
3476 routes[0].source = source->name;
3477 routes[0].sink = link_name;
3478 routes[1].source = link_name;
3479 routes[1].sink = sink->name;
3480
3481 return snd_soc_dapm_add_routes(&card->dapm, routes,
3482 ARRAY_SIZE(routes));
3483}
3484
Mark Brown888df3952012-02-16 19:37:51 -08003485int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3486 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003487{
Mark Brown888df3952012-02-16 19:37:51 -08003488 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003489 struct snd_soc_dapm_widget *w;
3490
Mark Brown888df3952012-02-16 19:37:51 -08003491 WARN_ON(dapm->dev != dai->dev);
3492
3493 memset(&template, 0, sizeof(template));
3494 template.reg = SND_SOC_NOPM;
3495
3496 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003497 template.id = snd_soc_dapm_dai_in;
Mark Brown888df3952012-02-16 19:37:51 -08003498 template.name = dai->driver->playback.stream_name;
3499 template.sname = dai->driver->playback.stream_name;
3500
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003501 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003502 template.name);
3503
3504 w = snd_soc_dapm_new_control(dapm, &template);
3505 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003506 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003507 dai->driver->playback.stream_name);
3508 }
3509
3510 w->priv = dai;
3511 dai->playback_widget = w;
3512 }
3513
3514 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003515 template.id = snd_soc_dapm_dai_out;
Mark Brown888df3952012-02-16 19:37:51 -08003516 template.name = dai->driver->capture.stream_name;
3517 template.sname = dai->driver->capture.stream_name;
3518
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003519 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003520 template.name);
3521
3522 w = snd_soc_dapm_new_control(dapm, &template);
3523 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003524 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df3952012-02-16 19:37:51 -08003525 dai->driver->capture.stream_name);
3526 }
3527
3528 w->priv = dai;
3529 dai->capture_widget = w;
3530 }
3531
3532 return 0;
3533}
3534
3535int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3536{
3537 struct snd_soc_dapm_widget *dai_w, *w;
3538 struct snd_soc_dai *dai;
Mark Brown888df3952012-02-16 19:37:51 -08003539
3540 /* For each DAI widget... */
3541 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003542 switch (dai_w->id) {
3543 case snd_soc_dapm_dai_in:
3544 case snd_soc_dapm_dai_out:
3545 break;
3546 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003547 continue;
Mark Brown46162742013-06-05 19:36:11 +01003548 }
Mark Brown888df3952012-02-16 19:37:51 -08003549
3550 dai = dai_w->priv;
3551
3552 /* ...find all widgets with the same stream and link them */
3553 list_for_each_entry(w, &card->widgets, list) {
3554 if (w->dapm != dai_w->dapm)
3555 continue;
3556
Mark Brown46162742013-06-05 19:36:11 +01003557 switch (w->id) {
3558 case snd_soc_dapm_dai_in:
3559 case snd_soc_dapm_dai_out:
Mark Brown888df3952012-02-16 19:37:51 -08003560 continue;
Mark Brown46162742013-06-05 19:36:11 +01003561 default:
3562 break;
3563 }
Mark Brown888df3952012-02-16 19:37:51 -08003564
Russell King19c2c5f2013-08-04 20:24:03 +01003565 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df3952012-02-16 19:37:51 -08003566 continue;
3567
3568 if (dai->driver->playback.stream_name &&
3569 strstr(w->sname,
3570 dai->driver->playback.stream_name)) {
Mark Brown888df3952012-02-16 19:37:51 -08003571 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003572 dai->playback_widget->name, w->name);
Mark Brown888df3952012-02-16 19:37:51 -08003573
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003574 snd_soc_dapm_add_path(w->dapm,
3575 dai->playback_widget, w, NULL, NULL);
Mark Brown888df3952012-02-16 19:37:51 -08003576 }
3577
3578 if (dai->driver->capture.stream_name &&
3579 strstr(w->sname,
3580 dai->driver->capture.stream_name)) {
Mark Brown888df3952012-02-16 19:37:51 -08003581 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003582 w->name, dai->capture_widget->name);
Mark Brown888df3952012-02-16 19:37:51 -08003583
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003584 snd_soc_dapm_add_path(w->dapm, w,
3585 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003586 }
3587 }
3588 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589
Mark Brown888df3952012-02-16 19:37:51 -08003590 return 0;
3591}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003592
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003593static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3594 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003595{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003596
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003597 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3598 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3599 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003600
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003601 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3602 w_cpu = cpu_dai->playback_widget;
3603 w_codec = codec_dai->playback_widget;
3604 } else {
3605 w_cpu = cpu_dai->capture_widget;
3606 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003607 }
3608
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003609 if (w_cpu) {
3610
3611 dapm_mark_dirty(w_cpu, "stream event");
3612
3613 switch (event) {
3614 case SND_SOC_DAPM_STREAM_START:
3615 w_cpu->active = 1;
3616 break;
3617 case SND_SOC_DAPM_STREAM_STOP:
3618 w_cpu->active = 0;
3619 break;
3620 case SND_SOC_DAPM_STREAM_SUSPEND:
3621 case SND_SOC_DAPM_STREAM_RESUME:
3622 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3623 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3624 break;
3625 }
3626 }
3627
3628 if (w_codec) {
3629
3630 dapm_mark_dirty(w_codec, "stream event");
3631
3632 switch (event) {
3633 case SND_SOC_DAPM_STREAM_START:
3634 w_codec->active = 1;
3635 break;
3636 case SND_SOC_DAPM_STREAM_STOP:
3637 w_codec->active = 0;
3638 break;
3639 case SND_SOC_DAPM_STREAM_SUSPEND:
3640 case SND_SOC_DAPM_STREAM_RESUME:
3641 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3642 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3643 break;
3644 }
3645 }
3646
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003647 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003648}
3649
3650/**
3651 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3652 * @rtd: PCM runtime data
3653 * @stream: stream name
3654 * @event: stream event
3655 *
3656 * Sends a stream event to the dapm core. The core then makes any
3657 * necessary widget power changes.
3658 *
3659 * Returns 0 for success else error.
3660 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003661void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3662 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003663{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003664 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003665
Liam Girdwood3cd04342012-03-09 12:02:08 +00003666 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003667 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003668 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003669}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003670
3671/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003672 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003673 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003674 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003675 *
Mark Brown74b8f952009-06-06 11:26:15 +01003676 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003677 * a valid audio route and active audio stream.
3678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3679 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003680 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003681int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003682{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003683 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003684}
Liam Girdwooda5302182008-07-07 13:35:17 +01003685EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003686
3687/**
Mark Brownda341832010-03-15 19:23:37 +00003688 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003689 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003690 * @pin: pin name
3691 *
3692 * Enables input/output pin regardless of any other state. This is
3693 * intended for use with microphone bias supplies used in microphone
3694 * jack detection.
3695 *
3696 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3697 * do any widget power switching.
3698 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003699int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3700 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003701{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003702 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003703
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003704 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003705 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003706 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003707 }
3708
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003709 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003710 w->connected = 1;
3711 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003712 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003713
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003714 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003715}
3716EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3717
3718/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003719 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003720 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003721 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003722 *
Mark Brown74b8f952009-06-06 11:26:15 +01003723 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003724 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3725 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003726 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003727int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3728 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003729{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003730 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003731}
3732EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3733
3734/**
Mark Brown5817b522008-09-24 11:23:11 +01003735 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003736 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003737 * @pin: pin name
3738 *
3739 * Marks the specified pin as being not connected, disabling it along
3740 * any parent or child widgets. At present this is identical to
3741 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3742 * additional things such as disabling controls which only affect
3743 * paths through the pin.
3744 *
3745 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3746 * do any widget power switching.
3747 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003748int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003749{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003750 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003751}
3752EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3753
3754/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003755 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003756 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003757 * @pin: audio signal pin endpoint (or start point)
3758 *
3759 * Get audio pin status - connected or disconnected.
3760 *
3761 * Returns 1 for connected otherwise 0.
3762 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003763int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3764 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003765{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003766 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003767
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003768 if (w)
3769 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003770
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003771 return 0;
3772}
Liam Girdwooda5302182008-07-07 13:35:17 +01003773EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003774
3775/**
Mark Brown1547aba2010-05-07 21:11:40 +01003776 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003777 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003778 * @pin: audio signal pin endpoint (or start point)
3779 *
3780 * Mark the given endpoint or pin as ignoring suspend. When the
3781 * system is disabled a path between two endpoints flagged as ignoring
3782 * suspend will not be disabled. The path must already be enabled via
3783 * normal means at suspend time, it will not be turned on if it was not
3784 * already enabled.
3785 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003786int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3787 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003788{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003789 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003790
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003791 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003792 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003793 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003794 }
3795
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003796 w->ignore_suspend = 1;
3797
3798 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003799}
3800EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3801
Stephen Warren16332812011-11-23 12:42:04 -07003802static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3803 struct snd_soc_dapm_widget *w)
3804{
3805 struct snd_soc_dapm_path *p;
3806
3807 list_for_each_entry(p, &card->paths, list) {
3808 if ((p->source == w) || (p->sink == w)) {
3809 dev_dbg(card->dev,
3810 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3811 p->source->name, p->source->id, p->source->dapm,
3812 p->sink->name, p->sink->id, p->sink->dapm);
3813
3814 /* Connected to something other than the codec */
3815 if (p->source->dapm != p->sink->dapm)
3816 return true;
3817 /*
3818 * Loopback connection from codec external pin to
3819 * codec external pin
3820 */
3821 if (p->sink->id == snd_soc_dapm_input) {
3822 switch (p->source->id) {
3823 case snd_soc_dapm_output:
3824 case snd_soc_dapm_micbias:
3825 return true;
3826 default:
3827 break;
3828 }
3829 }
3830 }
3831 }
3832
3833 return false;
3834}
3835
3836/**
3837 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3838 * @codec: The codec whose pins should be processed
3839 *
3840 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3841 * which are unused. Pins are used if they are connected externally to the
3842 * codec, whether that be to some other device, or a loop-back connection to
3843 * the codec itself.
3844 */
3845void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3846{
3847 struct snd_soc_card *card = codec->card;
3848 struct snd_soc_dapm_context *dapm = &codec->dapm;
3849 struct snd_soc_dapm_widget *w;
3850
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003851 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003852 &card->dapm, &codec->dapm);
3853
3854 list_for_each_entry(w, &card->widgets, list) {
3855 if (w->dapm != dapm)
3856 continue;
3857 switch (w->id) {
3858 case snd_soc_dapm_input:
3859 case snd_soc_dapm_output:
3860 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003861 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003862 w->name);
3863 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003864 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003865 "... Not in map; disabling\n");
3866 snd_soc_dapm_nc_pin(dapm, w->name);
3867 }
3868 break;
3869 default:
3870 break;
3871 }
3872 }
3873}
3874
Mark Brown1547aba2010-05-07 21:11:40 +01003875/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003876 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003877 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003878 *
3879 * Free all dapm widgets and resources.
3880 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003881void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003882{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003883 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003884 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003885 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003886 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003887}
3888EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3889
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003890static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003891{
Liam Girdwood01005a72012-07-06 16:57:05 +01003892 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003893 struct snd_soc_dapm_widget *w;
3894 LIST_HEAD(down_list);
3895 int powerdown = 0;
3896
Liam Girdwood01005a72012-07-06 16:57:05 +01003897 mutex_lock(&card->dapm_mutex);
3898
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003899 list_for_each_entry(w, &dapm->card->widgets, list) {
3900 if (w->dapm != dapm)
3901 continue;
Mark Brown51737472009-06-22 13:16:51 +01003902 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003903 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003904 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003905 powerdown = 1;
3906 }
3907 }
3908
3909 /* If there were no widgets to power down we're already in
3910 * standby.
3911 */
3912 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003913 if (dapm->bias_level == SND_SOC_BIAS_ON)
3914 snd_soc_dapm_set_bias_level(dapm,
3915 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003916 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003917 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3918 snd_soc_dapm_set_bias_level(dapm,
3919 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003920 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003921
3922 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003923}
Mark Brown51737472009-06-22 13:16:51 +01003924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925/*
3926 * snd_soc_dapm_shutdown - callback for system shutdown
3927 */
3928void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3929{
3930 struct snd_soc_codec *codec;
3931
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003932 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003933 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003934 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3935 snd_soc_dapm_set_bias_level(&codec->dapm,
3936 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003937 }
Mark Brown51737472009-06-22 13:16:51 +01003938}
3939
Richard Purdie2b97eab2006-10-06 18:32:18 +02003940/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003941MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003942MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3943MODULE_LICENSE("GPL");