blob: b885a9bedc4eac34eea1734edac4802246105e3a [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
232 data->widget = snd_soc_dapm_new_control(widget->dapm,
233 &template);
234 if (!data->widget) {
235 kfree(data);
236 return -ENOMEM;
237 }
238 }
239 break;
240 default:
241 break;
242 }
243
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200244 kcontrol->private_data = data;
245
246 return 0;
247}
248
249static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
250{
251 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200252 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200253 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200254 kfree(data);
255}
256
257static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
258 const struct snd_kcontrol *kcontrol)
259{
260 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
261
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200262 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200263}
264
265static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
266 struct snd_soc_dapm_widget *widget)
267{
268 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200269 struct snd_soc_dapm_widget_list *new_wlist;
270 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200271
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200272 if (data->wlist)
273 n = data->wlist->num_widgets + 1;
274 else
275 n = 1;
276
277 new_wlist = krealloc(data->wlist,
278 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
279 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200280 return -ENOMEM;
281
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200282 new_wlist->widgets[n - 1] = widget;
283 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200285 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200286
287 return 0;
288}
289
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200290static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
291 struct snd_soc_dapm_path *path)
292{
293 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
294
295 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200296
297 if (data->widget) {
298 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
299 path->source, NULL, NULL);
300 }
301}
302
303static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
304{
305 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
306
307 if (!data->widget)
308 return true;
309
310 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200311}
312
313static struct list_head *dapm_kcontrol_get_path_list(
314 const struct snd_kcontrol *kcontrol)
315{
316 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
317
318 return &data->paths;
319}
320
321#define dapm_kcontrol_for_each_path(path, kcontrol) \
322 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
323 list_kcontrol)
324
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200325static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
326{
327 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
328
329 return data->value;
330}
331
332static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
333 unsigned int value)
334{
335 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
336
337 if (data->value == value)
338 return false;
339
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200340 if (data->widget)
341 data->widget->on_val = value;
342
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200343 data->value = value;
344
345 return true;
346}
347
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200348/**
349 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
350 * @kcontrol: The kcontrol
351 */
352struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
353{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200354 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200355}
356EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
357
Liam Girdwood6c120e12012-02-15 15:15:34 +0000358static void dapm_reset(struct snd_soc_card *card)
359{
360 struct snd_soc_dapm_widget *w;
361
362 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
363
364 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200365 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000366 w->power_checked = false;
367 w->inputs = -1;
368 w->outputs = -1;
369 }
370}
371
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100372static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
373{
374 if (w->codec)
375 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100376 else if (w->platform)
377 return snd_soc_platform_read(w->platform, reg);
378
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000379 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100380 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100381}
382
383static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
384{
385 if (w->codec)
386 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100387 else if (w->platform)
388 return snd_soc_platform_write(w->platform, reg, val);
389
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000390 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100391 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100392}
393
Liam Girdwood49575fb52012-03-06 18:16:19 +0000394static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
395{
Mark Browne06ab3b2012-03-06 23:58:22 +0000396 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000397 mutex_lock(&w->codec->mutex);
398 else if (w->platform)
399 mutex_lock(&w->platform->mutex);
400}
401
402static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
403{
Mark Browne06ab3b2012-03-06 23:58:22 +0000404 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000405 mutex_unlock(&w->codec->mutex);
406 else if (w->platform)
407 mutex_unlock(&w->platform->mutex);
408}
409
410static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100411 unsigned short reg, unsigned int mask, unsigned int value)
412{
Mark Brown8a713da2011-12-03 12:33:55 +0000413 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100414 unsigned int old, new;
415 int ret;
416
Mark Brown8a713da2011-12-03 12:33:55 +0000417 if (w->codec && w->codec->using_regmap) {
418 ret = regmap_update_bits_check(w->codec->control_data,
419 reg, mask, value, &change);
420 if (ret != 0)
421 return ret;
422 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000423 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000424 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000425 if (ret < 0) {
426 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100427 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000428 }
Mark Brown8a713da2011-12-03 12:33:55 +0000429
430 old = ret;
431 new = (old & ~mask) | (value & mask);
432 change = old != new;
433 if (change) {
434 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000435 if (ret < 0) {
436 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000437 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000438 }
Mark Brown8a713da2011-12-03 12:33:55 +0000439 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000440 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100441 }
442
443 return change;
444}
445
Mark Brown452c5ea2009-05-17 21:41:23 +0100446/**
447 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800448 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100449 * @level: level to configure
450 *
451 * Configure the bias (power) levels for the SoC audio device.
452 *
453 * Returns 0 for success else error.
454 */
Mark Browned5a4c42011-02-18 11:12:42 -0800455static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200456 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100457{
Mark Browned5a4c42011-02-18 11:12:42 -0800458 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100459 int ret = 0;
460
Mark Brown84e90932010-11-04 00:07:02 -0400461 trace_snd_soc_bias_level_start(card, level);
462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100464 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100465 if (ret != 0)
466 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100467
Mark Browncc4c6702011-06-06 19:03:34 +0100468 if (dapm->codec) {
469 if (dapm->codec->driver->set_bias_level)
470 ret = dapm->codec->driver->set_bias_level(dapm->codec,
471 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100472 else
473 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100474 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100475 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100476 }
Liam Girdwood41231282012-07-06 16:56:16 +0100477
Mark Brown171ec6b2011-06-06 18:15:19 +0100478 if (ret != 0)
479 goto out;
480
481 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100482 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100483out:
Mark Brown84e90932010-11-04 00:07:02 -0400484 trace_snd_soc_bias_level_done(card, level);
485
Mark Brown452c5ea2009-05-17 21:41:23 +0100486 return ret;
487}
488
Richard Purdie2b97eab2006-10-06 18:32:18 +0200489/* set up initial codec paths */
490static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
491 struct snd_soc_dapm_path *p, int i)
492{
493 switch (w->id) {
494 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000495 case snd_soc_dapm_mixer:
496 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100498 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600499 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400500 unsigned int reg = mc->reg;
501 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100502 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400503 unsigned int mask = (1 << fls(max)) - 1;
504 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200505
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100506 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200508 if (invert)
509 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200510
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200511 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512 }
513 break;
514 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600515 struct soc_enum *e = (struct soc_enum *)
516 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200517 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100519 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200520 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200522 if (item < e->max && !strcmp(p->name, e->texts[item]))
523 p->connect = 1;
524 else
525 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 }
527 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000528 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600529 struct soc_enum *e = (struct soc_enum *)
530 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000531
532 p->connect = 0;
533 /* since a virtual mux has no backing registers to
534 * decide which path to connect, it will try to match
535 * with the first enumeration. This is to ensure
536 * that the default mux choice (the first) will be
537 * correctly powered up during initialization.
538 */
539 if (!strcmp(p->name, e->texts[0]))
540 p->connect = 1;
541 }
542 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200543 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200544 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600545 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200546 int val, item;
547
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100548 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200549 val = (val >> e->shift_l) & e->mask;
550 for (item = 0; item < e->max; item++) {
551 if (val == e->values[item])
552 break;
553 }
554
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200555 if (item < e->max && !strcmp(p->name, e->texts[item]))
556 p->connect = 1;
557 else
558 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200559 }
560 break;
Mark Brown56563102011-10-03 22:41:09 +0100561 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600563 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200564 case snd_soc_dapm_output:
565 case snd_soc_dapm_adc:
566 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000567 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200568 case snd_soc_dapm_dac:
569 case snd_soc_dapm_micbias:
570 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100571 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000572 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200573 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100574 case snd_soc_dapm_aif_in:
575 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100576 case snd_soc_dapm_dai_in:
577 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 case snd_soc_dapm_hp:
579 case snd_soc_dapm_mic:
580 case snd_soc_dapm_spk:
581 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100582 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200583 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100584 p->connect = 1;
585 break;
586 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587 case snd_soc_dapm_pre:
588 case snd_soc_dapm_post:
589 p->connect = 0;
590 break;
591 }
592}
593
Mark Brown74b8f952009-06-06 11:26:15 +0100594/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200596 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
597 struct snd_soc_dapm_path *path, const char *control_name,
598 const struct snd_kcontrol_new *kcontrol)
599{
600 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
601 int i;
602
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100603 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200605 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200606 list_add(&path->list_sink, &dest->sources);
607 list_add(&path->list_source, &src->sinks);
608 path->name = (char*)e->texts[i];
609 dapm_set_path_status(dest, path, 0);
610 return 0;
611 }
612 }
613
614 return -ENODEV;
615}
616
Mark Brown74b8f952009-06-06 11:26:15 +0100617/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200618static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
620 struct snd_soc_dapm_path *path, const char *control_name)
621{
622 int i;
623
624 /* search for mixer kcontrol */
625 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600626 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200627 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200628 list_add(&path->list_sink, &dest->sources);
629 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600630 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 dapm_set_path_status(dest, path, i);
632 return 0;
633 }
634 }
635 return -ENODEV;
636}
637
Stephen Warrenaf468002011-04-28 17:38:01 -0600638static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600639 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600640 const struct snd_kcontrol_new *kcontrol_new,
641 struct snd_kcontrol **kcontrol)
642{
643 struct snd_soc_dapm_widget *w;
644 int i;
645
646 *kcontrol = NULL;
647
648 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600649 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
650 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600651 for (i = 0; i < w->num_kcontrols; i++) {
652 if (&w->kcontrol_news[i] == kcontrol_new) {
653 if (w->kcontrols)
654 *kcontrol = w->kcontrols[i];
655 return 1;
656 }
657 }
658 }
659
660 return 0;
661}
662
Stephen Warren85762e72013-03-29 15:40:10 -0600663/*
664 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
665 * create it. Either way, add the widget into the control's widget list
666 */
667static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100668 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200669{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200670 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000671 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000672 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600673 size_t prefix_len;
674 int shared;
675 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600676 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600677 char *long_name;
678 const char *name;
679 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000680
681 if (dapm->codec)
682 prefix = dapm->codec->name_prefix;
683 else
684 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000686 if (prefix)
687 prefix_len = strlen(prefix) + 1;
688 else
689 prefix_len = 0;
690
Stephen Warren85762e72013-03-29 15:40:10 -0600691 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
692 &kcontrol);
693
Stephen Warren85762e72013-03-29 15:40:10 -0600694 if (!kcontrol) {
695 if (shared) {
696 wname_in_long_name = false;
697 kcname_in_long_name = true;
698 } else {
699 switch (w->id) {
700 case snd_soc_dapm_switch:
701 case snd_soc_dapm_mixer:
702 wname_in_long_name = true;
703 kcname_in_long_name = true;
704 break;
705 case snd_soc_dapm_mixer_named_ctl:
706 wname_in_long_name = false;
707 kcname_in_long_name = true;
708 break;
709 case snd_soc_dapm_mux:
710 case snd_soc_dapm_virt_mux:
711 case snd_soc_dapm_value_mux:
712 wname_in_long_name = true;
713 kcname_in_long_name = false;
714 break;
715 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600716 return -EINVAL;
717 }
718 }
719
720 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600721 /*
722 * The control will get a prefix from the control
723 * creation process but we're also using the same
724 * prefix for widgets so cut the prefix off the
725 * front of the widget name.
726 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200727 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600728 w->name + prefix_len,
729 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200730 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200731 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600732
733 name = long_name;
734 } else if (wname_in_long_name) {
735 long_name = NULL;
736 name = w->name + prefix_len;
737 } else {
738 long_name = NULL;
739 name = w->kcontrol_news[kci].name;
740 }
741
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200742 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600743 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200744 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200745 if (!kcontrol)
746 return -ENOMEM;
747 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200748
749 ret = dapm_kcontrol_data_alloc(w, kcontrol);
750 if (ret) {
751 snd_ctl_free_one(kcontrol);
752 return ret;
753 }
754
Stephen Warren85762e72013-03-29 15:40:10 -0600755 ret = snd_ctl_add(card, kcontrol);
756 if (ret < 0) {
757 dev_err(dapm->dev,
758 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
759 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600760 return ret;
761 }
Stephen Warren85762e72013-03-29 15:40:10 -0600762 }
763
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200764 ret = dapm_kcontrol_add_widget(kcontrol, w);
765 if (ret)
766 return ret;
767
Stephen Warren85762e72013-03-29 15:40:10 -0600768 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600769
770 return 0;
771}
772
773/* create new dapm mixer control */
774static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
775{
776 int i, ret;
777 struct snd_soc_dapm_path *path;
778
Richard Purdie2b97eab2006-10-06 18:32:18 +0200779 /* add kcontrol */
780 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781 /* match name */
782 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200783 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600784 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 continue;
786
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200787 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200788 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200789 continue;
790 }
791
Mark Brown946d92a2013-08-12 23:28:42 +0100792 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600793 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200794 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100795
796 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797 }
798 }
Stephen Warren85762e72013-03-29 15:40:10 -0600799
800 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801}
802
803/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200804static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200805{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200806 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600807 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600808 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200809
Stephen Warrenaf468002011-04-28 17:38:01 -0600810 if (w->num_kcontrols != 1) {
811 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000812 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600813 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814 return -EINVAL;
815 }
816
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200817 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600818 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
819 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600820 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200821
Mark Brown946d92a2013-08-12 23:28:42 +0100822 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600823 if (ret < 0)
824 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600825
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200827 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828
Stephen Warrenaf468002011-04-28 17:38:01 -0600829 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200830}
831
832/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200833static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834{
Mark Browna6c65732010-03-03 17:45:21 +0000835 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200836 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000837 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838
Mark Browna6c65732010-03-03 17:45:21 +0000839 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840}
841
842/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100843static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
844 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845{
846 struct snd_soc_dapm_path *p;
847
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100848 list_for_each_entry(p, sink, list_source) {
849 if (p->walked) {
850 p->walked = 0;
851 dapm_clear_walk_output(dapm, &p->sink->sinks);
852 }
853 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854}
855
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100856static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
857 struct list_head *source)
858{
859 struct snd_soc_dapm_path *p;
860
861 list_for_each_entry(p, source, list_sink) {
862 if (p->walked) {
863 p->walked = 0;
864 dapm_clear_walk_input(dapm, &p->source->sources);
865 }
866 }
867}
868
869
Mark Brown99497882010-05-07 20:24:05 +0100870/* We implement power down on suspend by checking the power state of
871 * the ALSA card - when we are suspending the ALSA state for the card
872 * is set to D3.
873 */
874static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
875{
Mark Brown12ea2c72011-03-02 18:17:32 +0000876 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100877
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100879 case SNDRV_CTL_POWER_D3hot:
880 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100881 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000882 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200883 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100884 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100885 default:
886 return 1;
887 }
888}
889
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100890/* add widget to list if it's not already in the list */
891static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
892 struct snd_soc_dapm_widget *w)
893{
894 struct snd_soc_dapm_widget_list *wlist;
895 int wlistsize, wlistentries, i;
896
897 if (*list == NULL)
898 return -EINVAL;
899
900 wlist = *list;
901
902 /* is this widget already in the list */
903 for (i = 0; i < wlist->num_widgets; i++) {
904 if (wlist->widgets[i] == w)
905 return 0;
906 }
907
908 /* allocate some new space */
909 wlistentries = wlist->num_widgets + 1;
910 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
911 wlistentries * sizeof(struct snd_soc_dapm_widget *);
912 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
913 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000914 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100915 w->name);
916 return -ENOMEM;
917 }
918 wlist = *list;
919
920 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000921 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100922 w->name, wlist->num_widgets);
923
924 wlist->widgets[wlist->num_widgets] = w;
925 wlist->num_widgets++;
926 return 1;
927}
928
Richard Purdie2b97eab2006-10-06 18:32:18 +0200929/*
930 * Recursively check for a completed path to an active or physically connected
931 * output widget. Returns number of complete paths.
932 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100933static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
934 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935{
936 struct snd_soc_dapm_path *path;
937 int con = 0;
938
Mark Brown024dc072011-10-09 11:52:05 +0100939 if (widget->outputs >= 0)
940 return widget->outputs;
941
Mark Brownde02d072011-09-20 21:43:24 +0100942 DAPM_UPDATE_STAT(widget, path_checks);
943
Mark Brown62ea8742012-01-21 21:14:48 +0000944 switch (widget->id) {
945 case snd_soc_dapm_supply:
946 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200947 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200948 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100949 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000950 default:
951 break;
952 }
Mark Brown246d0a12009-04-22 18:24:55 +0100953
Mark Brown010ff262009-08-17 17:39:22 +0100954 switch (widget->id) {
955 case snd_soc_dapm_adc:
956 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100957 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100958 if (widget->active) {
959 widget->outputs = snd_soc_dapm_suspend_check(widget);
960 return widget->outputs;
961 }
Mark Brown010ff262009-08-17 17:39:22 +0100962 default:
963 break;
964 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200965
966 if (widget->connected) {
967 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100968 if (widget->id == snd_soc_dapm_output && !widget->ext) {
969 widget->outputs = snd_soc_dapm_suspend_check(widget);
970 return widget->outputs;
971 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200972
973 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100974 if (widget->id == snd_soc_dapm_hp ||
975 widget->id == snd_soc_dapm_spk ||
976 (widget->id == snd_soc_dapm_line &&
977 !list_empty(&widget->sources))) {
978 widget->outputs = snd_soc_dapm_suspend_check(widget);
979 return widget->outputs;
980 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200981 }
982
983 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100984 DAPM_UPDATE_STAT(widget, neighbour_checks);
985
Mark Brownbf3a9e12011-06-13 16:42:29 +0100986 if (path->weak)
987 continue;
988
Mark Brown8af294b2013-02-22 17:48:15 +0000989 if (path->walking)
990 return 1;
991
Richard Purdie2b97eab2006-10-06 18:32:18 +0200992 if (path->walked)
993 continue;
994
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100995 trace_snd_soc_dapm_output_path(widget, path);
996
Richard Purdie2b97eab2006-10-06 18:32:18 +0200997 if (path->sink && path->connect) {
998 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000999 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001000
1001 /* do we need to add this widget to the list ? */
1002 if (list) {
1003 int err;
1004 err = dapm_list_add_widget(list, path->sink);
1005 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001006 dev_err(widget->dapm->dev,
1007 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001008 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001009 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001010 return con;
1011 }
1012 }
1013
1014 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001015
1016 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001017 }
1018 }
1019
Mark Brown024dc072011-10-09 11:52:05 +01001020 widget->outputs = con;
1021
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022 return con;
1023}
1024
1025/*
1026 * Recursively check for a completed path to an active or physically connected
1027 * input widget. Returns number of complete paths.
1028 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001029static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1030 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001031{
1032 struct snd_soc_dapm_path *path;
1033 int con = 0;
1034
Mark Brown024dc072011-10-09 11:52:05 +01001035 if (widget->inputs >= 0)
1036 return widget->inputs;
1037
Mark Brownde02d072011-09-20 21:43:24 +01001038 DAPM_UPDATE_STAT(widget, path_checks);
1039
Mark Brown62ea8742012-01-21 21:14:48 +00001040 switch (widget->id) {
1041 case snd_soc_dapm_supply:
1042 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001043 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001044 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001045 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001046 default:
1047 break;
1048 }
Mark Brown246d0a12009-04-22 18:24:55 +01001049
Richard Purdie2b97eab2006-10-06 18:32:18 +02001050 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001051 switch (widget->id) {
1052 case snd_soc_dapm_dac:
1053 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001054 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001055 if (widget->active) {
1056 widget->inputs = snd_soc_dapm_suspend_check(widget);
1057 return widget->inputs;
1058 }
Mark Brown010ff262009-08-17 17:39:22 +01001059 default:
1060 break;
1061 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001062
1063 if (widget->connected) {
1064 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001065 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1066 widget->inputs = snd_soc_dapm_suspend_check(widget);
1067 return widget->inputs;
1068 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001069
1070 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001071 if (widget->id == snd_soc_dapm_vmid) {
1072 widget->inputs = snd_soc_dapm_suspend_check(widget);
1073 return widget->inputs;
1074 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001075
1076 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001077 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001078 (widget->id == snd_soc_dapm_line &&
1079 !list_empty(&widget->sinks))) {
1080 widget->inputs = snd_soc_dapm_suspend_check(widget);
1081 return widget->inputs;
1082 }
1083
Mark Brown1ab97c82011-11-27 16:21:51 +00001084 /* signal generator */
1085 if (widget->id == snd_soc_dapm_siggen) {
1086 widget->inputs = snd_soc_dapm_suspend_check(widget);
1087 return widget->inputs;
1088 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001089 }
1090
1091 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001092 DAPM_UPDATE_STAT(widget, neighbour_checks);
1093
Mark Brownbf3a9e12011-06-13 16:42:29 +01001094 if (path->weak)
1095 continue;
1096
Mark Brown8af294b2013-02-22 17:48:15 +00001097 if (path->walking)
1098 return 1;
1099
Richard Purdie2b97eab2006-10-06 18:32:18 +02001100 if (path->walked)
1101 continue;
1102
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001103 trace_snd_soc_dapm_input_path(widget, path);
1104
Richard Purdie2b97eab2006-10-06 18:32:18 +02001105 if (path->source && path->connect) {
1106 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001107 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001108
1109 /* do we need to add this widget to the list ? */
1110 if (list) {
1111 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001112 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001113 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001114 dev_err(widget->dapm->dev,
1115 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001116 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001117 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001118 return con;
1119 }
1120 }
1121
1122 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001123
1124 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001125 }
1126 }
1127
Mark Brown024dc072011-10-09 11:52:05 +01001128 widget->inputs = con;
1129
Richard Purdie2b97eab2006-10-06 18:32:18 +02001130 return con;
1131}
1132
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001133/**
1134 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1135 * @dai: the soc DAI.
1136 * @stream: stream direction.
1137 * @list: list of active widgets for this stream.
1138 *
1139 * Queries DAPM graph as to whether an valid audio stream path exists for
1140 * the initial stream specified by name. This takes into account
1141 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1142 *
1143 * Returns the number of valid paths or negative error.
1144 */
1145int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1146 struct snd_soc_dapm_widget_list **list)
1147{
1148 struct snd_soc_card *card = dai->card;
1149 int paths;
1150
1151 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1152 dapm_reset(card);
1153
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001154 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001155 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001156 dapm_clear_walk_output(&card->dapm,
1157 &dai->playback_widget->sinks);
1158 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001159 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001160 dapm_clear_walk_input(&card->dapm,
1161 &dai->capture_widget->sources);
1162 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001163
1164 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001165 mutex_unlock(&card->dapm_mutex);
1166
1167 return paths;
1168}
1169
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001171 * Handler for generic register modifier widget.
1172 */
1173int dapm_reg_event(struct snd_soc_dapm_widget *w,
1174 struct snd_kcontrol *kcontrol, int event)
1175{
1176 unsigned int val;
1177
1178 if (SND_SOC_DAPM_EVENT_ON(event))
1179 val = w->on_val;
1180 else
1181 val = w->off_val;
1182
Liam Girdwood49575fb52012-03-06 18:16:19 +00001183 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001184 w->mask << w->shift, val << w->shift);
1185
1186 return 0;
1187}
Mark Brown11589412008-07-29 11:42:23 +01001188EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001189
Mark Brown62ea8742012-01-21 21:14:48 +00001190/*
1191 * Handler for regulator supply widget.
1192 */
1193int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1194 struct snd_kcontrol *kcontrol, int event)
1195{
Mark Brownc05b84d2012-09-07 12:57:11 +08001196 int ret;
1197
1198 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001199 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001200 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001201 if (ret != 0)
1202 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001203 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001204 w->name, ret);
1205 }
1206
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001207 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001208 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001209 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001210 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001211 if (ret != 0)
1212 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001213 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001214 w->name, ret);
1215 }
1216
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001217 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001218 }
Mark Brown62ea8742012-01-21 21:14:48 +00001219}
1220EXPORT_SYMBOL_GPL(dapm_regulator_event);
1221
Ola Liljad7e7eb92012-05-24 15:26:25 +02001222/*
1223 * Handler for clock supply widget.
1224 */
1225int dapm_clock_event(struct snd_soc_dapm_widget *w,
1226 struct snd_kcontrol *kcontrol, int event)
1227{
1228 if (!w->clk)
1229 return -EIO;
1230
Mark Brownec029952012-06-04 08:16:20 +01001231#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001232 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001233 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001234 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001235 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001236 return 0;
1237 }
Mark Brownec029952012-06-04 08:16:20 +01001238#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001239 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001240}
1241EXPORT_SYMBOL_GPL(dapm_clock_event);
1242
Mark Brownd8050022011-09-28 18:28:23 +01001243static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1244{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001245 if (w->power_checked)
1246 return w->new_power;
1247
Mark Brownd8050022011-09-28 18:28:23 +01001248 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001249 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001250 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001251 w->new_power = w->power_check(w);
1252
1253 w->power_checked = true;
1254
1255 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001256}
1257
Mark Browncd0f2d42009-04-20 16:56:59 +01001258/* Generic check to see if a widget should be powered.
1259 */
1260static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1261{
1262 int in, out;
1263
Mark Brownde02d072011-09-20 21:43:24 +01001264 DAPM_UPDATE_STAT(w, power_checks);
1265
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001266 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001267 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001268 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001269 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001270 return out != 0 && in != 0;
1271}
1272
Mark Brown6ea31b92009-04-20 17:15:41 +01001273/* Check to see if an ADC has power */
1274static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1275{
1276 int in;
1277
Mark Brownde02d072011-09-20 21:43:24 +01001278 DAPM_UPDATE_STAT(w, power_checks);
1279
Mark Brown6ea31b92009-04-20 17:15:41 +01001280 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001281 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001282 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001283 return in != 0;
1284 } else {
1285 return dapm_generic_check_power(w);
1286 }
1287}
1288
1289/* Check to see if a DAC has power */
1290static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1291{
1292 int out;
1293
Mark Brownde02d072011-09-20 21:43:24 +01001294 DAPM_UPDATE_STAT(w, power_checks);
1295
Mark Brown6ea31b92009-04-20 17:15:41 +01001296 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001297 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001298 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001299 return out != 0;
1300 } else {
1301 return dapm_generic_check_power(w);
1302 }
1303}
1304
Mark Brown246d0a12009-04-22 18:24:55 +01001305/* Check to see if a power supply is needed */
1306static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1307{
1308 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001309
Mark Brownde02d072011-09-20 21:43:24 +01001310 DAPM_UPDATE_STAT(w, power_checks);
1311
Mark Brown246d0a12009-04-22 18:24:55 +01001312 /* Check if one of our outputs is connected */
1313 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001314 DAPM_UPDATE_STAT(w, neighbour_checks);
1315
Mark Brownbf3a9e12011-06-13 16:42:29 +01001316 if (path->weak)
1317 continue;
1318
Mark Brown215edda2009-09-08 18:59:05 +01001319 if (path->connected &&
1320 !path->connected(path->source, path->sink))
1321 continue;
1322
Mark Brown30173582011-02-11 11:42:19 +00001323 if (!path->sink)
1324 continue;
1325
Mark Brownf68d7e12011-10-04 22:57:50 +01001326 if (dapm_widget_power_check(path->sink))
1327 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001328 }
1329
Mark Brownf68d7e12011-10-04 22:57:50 +01001330 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001331}
1332
Mark Brown35c64bca2011-09-28 18:23:53 +01001333static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1334{
1335 return 1;
1336}
1337
Mark Brown38357ab2009-06-06 19:03:23 +01001338static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1339 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001340 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001341{
Mark Brown828a8422011-01-15 13:14:30 +00001342 int *sort;
1343
1344 if (power_up)
1345 sort = dapm_up_seq;
1346 else
1347 sort = dapm_down_seq;
1348
Mark Brown38357ab2009-06-06 19:03:23 +01001349 if (sort[a->id] != sort[b->id])
1350 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001351 if (a->subseq != b->subseq) {
1352 if (power_up)
1353 return a->subseq - b->subseq;
1354 else
1355 return b->subseq - a->subseq;
1356 }
Mark Brownb22ead22009-06-07 12:51:26 +01001357 if (a->reg != b->reg)
1358 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001359 if (a->dapm != b->dapm)
1360 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001361
Mark Brown38357ab2009-06-06 19:03:23 +01001362 return 0;
1363}
Mark Brown42aa3412009-03-01 19:21:10 +00001364
Mark Brown38357ab2009-06-06 19:03:23 +01001365/* Insert a widget in order into a DAPM power sequence. */
1366static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1367 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001368 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001369{
1370 struct snd_soc_dapm_widget *w;
1371
1372 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001373 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001374 list_add_tail(&new_widget->power_list, &w->power_list);
1375 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001376 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001377
Mark Brown38357ab2009-06-06 19:03:23 +01001378 list_add_tail(&new_widget->power_list, list);
1379}
Mark Brown42aa3412009-03-01 19:21:10 +00001380
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001381static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001382 struct snd_soc_dapm_widget *w, int event)
1383{
Mark Brown68f89ad2010-11-03 23:51:49 -04001384 const char *ev_name;
1385 int power, ret;
1386
1387 switch (event) {
1388 case SND_SOC_DAPM_PRE_PMU:
1389 ev_name = "PRE_PMU";
1390 power = 1;
1391 break;
1392 case SND_SOC_DAPM_POST_PMU:
1393 ev_name = "POST_PMU";
1394 power = 1;
1395 break;
1396 case SND_SOC_DAPM_PRE_PMD:
1397 ev_name = "PRE_PMD";
1398 power = 0;
1399 break;
1400 case SND_SOC_DAPM_POST_PMD:
1401 ev_name = "POST_PMD";
1402 power = 0;
1403 break;
Mark Brown80114122013-02-25 15:14:19 +00001404 case SND_SOC_DAPM_WILL_PMU:
1405 ev_name = "WILL_PMU";
1406 power = 1;
1407 break;
1408 case SND_SOC_DAPM_WILL_PMD:
1409 ev_name = "WILL_PMD";
1410 power = 0;
1411 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001412 default:
1413 BUG();
1414 return;
1415 }
1416
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001417 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001418 return;
1419
1420 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001421 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001422 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001423 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001424 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001425 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001427 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 ev_name, w->name, ret);
1429 }
1430}
1431
Mark Brownb22ead22009-06-07 12:51:26 +01001432/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001433static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001434 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001435{
Mark Brown68f89ad2010-11-03 23:51:49 -04001436 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001437 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001438 unsigned int value = 0;
1439 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001440
1441 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1442 power_list)->reg;
1443
1444 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001445 BUG_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001446 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001447
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001448 mask |= w->mask << w->shift;
1449 if (w->power)
1450 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001451 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001452 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001453
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001454 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001455 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1456 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001457
Mark Brown68f89ad2010-11-03 23:51:49 -04001458 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001459 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1460 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001461 }
1462
Mark Brown81628102009-06-07 13:21:24 +01001463 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001464 /* Any widget will do, they should all be updating the
1465 * same register.
1466 */
1467 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1468 power_list);
1469
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001470 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001471 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001472 value, mask, reg, card->pop_time);
1473 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001474 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001475 }
1476
1477 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001478 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1479 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001480 }
Mark Brown42aa3412009-03-01 19:21:10 +00001481}
1482
Mark Brownb22ead22009-06-07 12:51:26 +01001483/* Apply a DAPM power sequence.
1484 *
1485 * We walk over a pre-sorted list of widgets to apply power to. In
1486 * order to minimise the number of writes to the device required
1487 * multiple widgets will be updated in a single write where possible.
1488 * Currently anything that requires more than a single write is not
1489 * handled.
1490 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001491static void dapm_seq_run(struct snd_soc_card *card,
1492 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001493{
1494 struct snd_soc_dapm_widget *w, *n;
1495 LIST_HEAD(pending);
1496 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001497 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001498 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001499 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001500 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001501 int *sort;
1502
1503 if (power_up)
1504 sort = dapm_up_seq;
1505 else
1506 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001507
Mark Brownb22ead22009-06-07 12:51:26 +01001508 list_for_each_entry_safe(w, n, list, power_list) {
1509 ret = 0;
1510
1511 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001512 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001513 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001514 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001515 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001516
Mark Brown474b62d2011-01-18 16:14:44 +00001517 if (cur_dapm && cur_dapm->seq_notifier) {
1518 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1519 if (sort[i] == cur_sort)
1520 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001521 i,
1522 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001523 }
1524
Mark Brownb22ead22009-06-07 12:51:26 +01001525 INIT_LIST_HEAD(&pending);
1526 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001527 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001528 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001529 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001530 }
1531
Mark Brown163cac02009-06-07 10:12:52 +01001532 switch (w->id) {
1533 case snd_soc_dapm_pre:
1534 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001535 list_for_each_entry_safe_continue(w, n, list,
1536 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001537
Mark Brownb22ead22009-06-07 12:51:26 +01001538 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001539 ret = w->event(w,
1540 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001541 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001542 ret = w->event(w,
1543 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001544 break;
1545
1546 case snd_soc_dapm_post:
1547 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001548 list_for_each_entry_safe_continue(w, n, list,
1549 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001550
Mark Brownb22ead22009-06-07 12:51:26 +01001551 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001552 ret = w->event(w,
1553 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001554 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001555 ret = w->event(w,
1556 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001557 break;
1558
Mark Brown163cac02009-06-07 10:12:52 +01001559 default:
Mark Brown81628102009-06-07 13:21:24 +01001560 /* Queue it up for application */
1561 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001562 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001563 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001564 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001565 list_move(&w->power_list, &pending);
1566 break;
Mark Brown163cac02009-06-07 10:12:52 +01001567 }
Mark Brownb22ead22009-06-07 12:51:26 +01001568
1569 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001570 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001571 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001572 }
Mark Brownb22ead22009-06-07 12:51:26 +01001573
1574 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001575 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001576
1577 if (cur_dapm && cur_dapm->seq_notifier) {
1578 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1579 if (sort[i] == cur_sort)
1580 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001581 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001582 }
Mark Brown163cac02009-06-07 10:12:52 +01001583}
1584
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001585static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001586{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001587 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001588 struct snd_soc_dapm_widget_list *wlist;
1589 struct snd_soc_dapm_widget *w = NULL;
1590 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001591 int ret;
1592
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001593 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001594 return;
1595
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001596 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001597
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001598 for (wi = 0; wi < wlist->num_widgets; wi++) {
1599 w = wlist->widgets[wi];
1600
1601 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1602 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1603 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001604 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001605 w->name, ret);
1606 }
Mark Brown97404f22010-12-14 16:13:57 +00001607 }
1608
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001609 if (!w)
1610 return;
1611
Liam Girdwood49575fb52012-03-06 18:16:19 +00001612 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001613 update->val);
1614 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001615 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001616 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001617
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001618 for (wi = 0; wi < wlist->num_widgets; wi++) {
1619 w = wlist->widgets[wi];
1620
1621 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1622 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1623 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001624 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001625 w->name, ret);
1626 }
Mark Brown97404f22010-12-14 16:13:57 +00001627 }
1628}
1629
Mark Brown9d0624a2011-02-18 11:49:43 -08001630/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1631 * they're changing state.
1632 */
1633static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1634{
1635 struct snd_soc_dapm_context *d = data;
1636 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001637
Mark Brown56fba412011-06-04 11:25:10 +01001638 /* If we're off and we're not supposed to be go into STANDBY */
1639 if (d->bias_level == SND_SOC_BIAS_OFF &&
1640 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001641 if (d->dev)
1642 pm_runtime_get_sync(d->dev);
1643
Mark Brown9d0624a2011-02-18 11:49:43 -08001644 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1645 if (ret != 0)
1646 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001647 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001648 }
1649
Mark Brown56fba412011-06-04 11:25:10 +01001650 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1651 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001652 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1653 if (ret != 0)
1654 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001655 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001656 }
1657}
1658
1659/* Async callback run prior to DAPM sequences - brings to their final
1660 * state.
1661 */
1662static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1663{
1664 struct snd_soc_dapm_context *d = data;
1665 int ret;
1666
1667 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001668 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1669 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1670 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001671 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1672 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001673 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001674 ret);
1675 }
1676
1677 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001678 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1679 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001680 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1681 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001682 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1683 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001684
1685 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001686 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001687 }
1688
1689 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001690 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1691 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001692 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1693 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001694 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001695 ret);
1696 }
1697}
Mark Brown97404f22010-12-14 16:13:57 +00001698
Mark Brownfe4fda52011-10-03 22:36:57 +01001699static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1700 bool power, bool connect)
1701{
1702 /* If a connection is being made or broken then that update
1703 * will have marked the peer dirty, otherwise the widgets are
1704 * not connected and this update has no impact. */
1705 if (!connect)
1706 return;
1707
1708 /* If the peer is already in the state we're moving to then we
1709 * won't have an impact on it. */
1710 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001711 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001712}
1713
Mark Brown05623c42011-09-28 17:02:31 +01001714static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1715 struct list_head *up_list,
1716 struct list_head *down_list)
1717{
Mark Browndb432b42011-10-03 21:06:40 +01001718 struct snd_soc_dapm_path *path;
1719
Mark Brown05623c42011-09-28 17:02:31 +01001720 if (w->power == power)
1721 return;
1722
1723 trace_snd_soc_dapm_widget_power(w, power);
1724
Mark Browndb432b42011-10-03 21:06:40 +01001725 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001726 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001727 */
1728 list_for_each_entry(path, &w->sources, list_sink) {
1729 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001730 dapm_widget_set_peer_power(path->source, power,
1731 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001732 }
1733 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001734 switch (w->id) {
1735 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001736 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001737 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001738 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001739 /* Supplies can't affect their outputs, only their inputs */
1740 break;
1741 default:
1742 list_for_each_entry(path, &w->sinks, list_source) {
1743 if (path->sink) {
1744 dapm_widget_set_peer_power(path->sink, power,
1745 path->connect);
1746 }
Mark Browndb432b42011-10-03 21:06:40 +01001747 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001748 break;
Mark Browndb432b42011-10-03 21:06:40 +01001749 }
1750
Mark Brown05623c42011-09-28 17:02:31 +01001751 if (power)
1752 dapm_seq_insert(w, up_list, true);
1753 else
1754 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001755}
1756
Mark Brown7c81beb2011-09-20 22:22:32 +01001757static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1758 struct list_head *up_list,
1759 struct list_head *down_list)
1760{
Mark Brown7c81beb2011-09-20 22:22:32 +01001761 int power;
1762
1763 switch (w->id) {
1764 case snd_soc_dapm_pre:
1765 dapm_seq_insert(w, down_list, false);
1766 break;
1767 case snd_soc_dapm_post:
1768 dapm_seq_insert(w, up_list, true);
1769 break;
1770
1771 default:
Mark Brownd8050022011-09-28 18:28:23 +01001772 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001773
Mark Brown05623c42011-09-28 17:02:31 +01001774 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001775 break;
1776 }
1777}
1778
Mark Brown42aa3412009-03-01 19:21:10 +00001779/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001780 * Scan each dapm widget for complete audio path.
1781 * A complete path is a route that has valid endpoints i.e.:-
1782 *
1783 * o DAC to output pin.
1784 * o Input Pin to ADC.
1785 * o Input pin to Output pin (bypass, sidetone)
1786 * o DAC to ADC (loopback).
1787 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001788static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001789{
1790 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001791 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001792 LIST_HEAD(up_list);
1793 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001794 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001795 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001796
Mark Brown84e90932010-11-04 00:07:02 -04001797 trace_snd_soc_dapm_start(card);
1798
Mark Brown56fba412011-06-04 11:25:10 +01001799 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098b2012-03-08 15:06:09 +00001800 if (d->idle_bias_off)
1801 d->target_bias_level = SND_SOC_BIAS_OFF;
1802 else
1803 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001804 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001805
Liam Girdwood6c120e12012-02-15 15:15:34 +00001806 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001807
Mark Brown6d3ddc82009-05-16 17:47:29 +01001808 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001809 * lists indicating if they should be powered up or down. We
1810 * only check widgets that have been flagged as dirty but note
1811 * that new widgets may be added to the dirty list while we
1812 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001813 */
Mark Browndb432b42011-10-03 21:06:40 +01001814 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001815 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816 }
1817
Mark Brownf9de6d72011-09-28 17:19:47 +01001818 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001819 switch (w->id) {
1820 case snd_soc_dapm_pre:
1821 case snd_soc_dapm_post:
1822 /* These widgets always need to be powered */
1823 break;
1824 default:
1825 list_del_init(&w->dirty);
1826 break;
1827 }
Mark Browndb432b42011-10-03 21:06:40 +01001828
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001829 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001830 d = w->dapm;
1831
1832 /* Supplies and micbiases only bring the
1833 * context up to STANDBY as unless something
1834 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001835 * generally don't require full power. Signal
1836 * generators are virtual pins and have no
1837 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001838 */
1839 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001840 case snd_soc_dapm_siggen:
1841 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001842 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001843 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001844 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001845 case snd_soc_dapm_micbias:
1846 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1847 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1848 break;
1849 default:
1850 d->target_bias_level = SND_SOC_BIAS_ON;
1851 break;
1852 }
1853 }
1854
1855 }
1856
Mark Brown85a843c2011-09-21 21:29:47 +01001857 /* Force all contexts in the card to the same bias state if
1858 * they're not ground referenced.
1859 */
Mark Brown56fba412011-06-04 11:25:10 +01001860 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001861 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001862 if (d->target_bias_level > bias)
1863 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001864 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001865 if (!d->idle_bias_off)
1866 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001867
Mark Brownde02d072011-09-20 21:43:24 +01001868 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001869
Mark Brown9d0624a2011-02-18 11:49:43 -08001870 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001871 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001872 async_schedule_domain(dapm_pre_sequence_async, d,
1873 &async_domain);
1874 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001875
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001876 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001877 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001878 }
1879
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001880 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001881 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001882 }
1883
Mark Brown6d3ddc82009-05-16 17:47:29 +01001884 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001885 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001886
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001887 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001888
Mark Brown6d3ddc82009-05-16 17:47:29 +01001889 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001890 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891
Mark Brown9d0624a2011-02-18 11:49:43 -08001892 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001893 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001894 async_schedule_domain(dapm_post_sequence_async, d,
1895 &async_domain);
1896 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001897
Liam Girdwood8078d872012-02-15 15:15:35 +00001898 /* do we need to notify any clients that DAPM event is complete */
1899 list_for_each_entry(d, &card->dapm_list, list) {
1900 if (d->stream_event)
1901 d->stream_event(d, event);
1902 }
1903
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001904 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001905 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001906 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001907
Mark Brown84e90932010-11-04 00:07:02 -04001908 trace_snd_soc_dapm_done(card);
1909
Mark Brown42aa3412009-03-01 19:21:10 +00001910 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911}
1912
Mark Brown79fb9382009-08-21 16:38:13 +01001913#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001914static ssize_t dapm_widget_power_read_file(struct file *file,
1915 char __user *user_buf,
1916 size_t count, loff_t *ppos)
1917{
1918 struct snd_soc_dapm_widget *w = file->private_data;
1919 char *buf;
1920 int in, out;
1921 ssize_t ret;
1922 struct snd_soc_dapm_path *p = NULL;
1923
1924 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1925 if (!buf)
1926 return -ENOMEM;
1927
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001928 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001929 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001930 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001931 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001932
Mark Brownf13ebad2012-03-03 18:01:01 +00001933 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1934 w->name, w->power ? "On" : "Off",
1935 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001936
Mark Brownd033c362009-12-04 15:25:56 +00001937 if (w->reg >= 0)
1938 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001939 " - R%d(0x%x) mask 0x%x",
1940 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001941
1942 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1943
Mark Brown3eef08b2009-09-14 16:49:00 +01001944 if (w->sname)
1945 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1946 w->sname,
1947 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001948
1949 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001950 if (p->connected && !p->connected(w, p->sink))
1951 continue;
1952
Mark Brown79fb9382009-08-21 16:38:13 +01001953 if (p->connect)
1954 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001955 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001956 p->name ? p->name : "static",
1957 p->source->name);
1958 }
1959 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001960 if (p->connected && !p->connected(w, p->sink))
1961 continue;
1962
Mark Brown79fb9382009-08-21 16:38:13 +01001963 if (p->connect)
1964 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001965 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001966 p->name ? p->name : "static",
1967 p->sink->name);
1968 }
1969
1970 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1971
1972 kfree(buf);
1973 return ret;
1974}
1975
1976static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001977 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001978 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001979 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001980};
1981
Mark Brownef49e4f2011-04-04 20:48:13 +09001982static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1983 size_t count, loff_t *ppos)
1984{
1985 struct snd_soc_dapm_context *dapm = file->private_data;
1986 char *level;
1987
1988 switch (dapm->bias_level) {
1989 case SND_SOC_BIAS_ON:
1990 level = "On\n";
1991 break;
1992 case SND_SOC_BIAS_PREPARE:
1993 level = "Prepare\n";
1994 break;
1995 case SND_SOC_BIAS_STANDBY:
1996 level = "Standby\n";
1997 break;
1998 case SND_SOC_BIAS_OFF:
1999 level = "Off\n";
2000 break;
2001 default:
2002 BUG();
2003 level = "Unknown\n";
2004 break;
2005 }
2006
2007 return simple_read_from_buffer(user_buf, count, ppos, level,
2008 strlen(level));
2009}
2010
2011static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002012 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002013 .read = dapm_bias_read_file,
2014 .llseek = default_llseek,
2015};
2016
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002017void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2018 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002019{
Mark Brown79fb9382009-08-21 16:38:13 +01002020 struct dentry *d;
2021
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002022 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2023
2024 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002025 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002026 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002027 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002028 }
Mark Brown79fb9382009-08-21 16:38:13 +01002029
Mark Brownef49e4f2011-04-04 20:48:13 +09002030 d = debugfs_create_file("bias_level", 0444,
2031 dapm->debugfs_dapm, dapm,
2032 &dapm_bias_fops);
2033 if (!d)
2034 dev_warn(dapm->dev,
2035 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002036}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002037
2038static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2039{
2040 struct snd_soc_dapm_context *dapm = w->dapm;
2041 struct dentry *d;
2042
2043 if (!dapm->debugfs_dapm || !w->name)
2044 return;
2045
2046 d = debugfs_create_file(w->name, 0444,
2047 dapm->debugfs_dapm, w,
2048 &dapm_widget_power_fops);
2049 if (!d)
2050 dev_warn(w->dapm->dev,
2051 "ASoC: Failed to create %s debugfs file\n",
2052 w->name);
2053}
2054
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002055static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2056{
2057 debugfs_remove_recursive(dapm->debugfs_dapm);
2058}
2059
Mark Brown79fb9382009-08-21 16:38:13 +01002060#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002061void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2062 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002063{
2064}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002065
2066static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2067{
2068}
2069
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002070static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2071{
2072}
2073
Mark Brown79fb9382009-08-21 16:38:13 +01002074#endif
2075
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002077static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002078 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079{
2080 struct snd_soc_dapm_path *path;
2081 int found = 0;
2082
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002084 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002085 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002086 continue;
2087
2088 found = 1;
2089 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002090 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002091 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002092 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002093 } else {
2094 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002095 dapm_mark_dirty(path->source,
2096 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002098 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002099 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100 }
2101
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002102 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002103 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104
Liam Girdwood618dae12012-04-25 12:12:51 +01002105 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002107
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002108int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002109 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2110 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002111{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002112 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002113 int ret;
2114
Liam Girdwood3cd04342012-03-09 12:02:08 +00002115 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002116 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002117 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002118 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002119 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002120 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002121 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002122 return ret;
2123}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002124EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125
Milan plzik1b075e32008-01-10 14:39:46 +01002126/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002127static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002128 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129{
2130 struct snd_soc_dapm_path *path;
2131 int found = 0;
2132
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002134 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002136 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002137 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002138 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 }
2140
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002141 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002142 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143
Liam Girdwood618dae12012-04-25 12:12:51 +01002144 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002146
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002147int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002148 struct snd_kcontrol *kcontrol, int connect,
2149 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002150{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002151 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002152 int ret;
2153
Liam Girdwood3cd04342012-03-09 12:02:08 +00002154 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002155 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002156 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002157 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002158 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002159 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002160 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002161 return ret;
2162}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002163EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164
2165/* show dapm widget status in sys fs */
2166static ssize_t dapm_widget_show(struct device *dev,
2167 struct device_attribute *attr, char *buf)
2168{
Mark Brown36ae1a92012-01-06 17:12:45 -08002169 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002170 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 struct snd_soc_dapm_widget *w;
2172 int count = 0;
2173 char *state = "not set";
2174
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002175 list_for_each_entry(w, &codec->card->widgets, list) {
2176 if (w->dapm != &codec->dapm)
2177 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178
2179 /* only display widgets that burnm power */
2180 switch (w->id) {
2181 case snd_soc_dapm_hp:
2182 case snd_soc_dapm_mic:
2183 case snd_soc_dapm_spk:
2184 case snd_soc_dapm_line:
2185 case snd_soc_dapm_micbias:
2186 case snd_soc_dapm_dac:
2187 case snd_soc_dapm_adc:
2188 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002189 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002191 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002192 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002193 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002194 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195 if (w->name)
2196 count += sprintf(buf + count, "%s: %s\n",
2197 w->name, w->power ? "On":"Off");
2198 break;
2199 default:
2200 break;
2201 }
2202 }
2203
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002204 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002205 case SND_SOC_BIAS_ON:
2206 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207 break;
Mark Brown0be98982008-05-19 12:31:28 +02002208 case SND_SOC_BIAS_PREPARE:
2209 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 break;
Mark Brown0be98982008-05-19 12:31:28 +02002211 case SND_SOC_BIAS_STANDBY:
2212 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213 break;
Mark Brown0be98982008-05-19 12:31:28 +02002214 case SND_SOC_BIAS_OFF:
2215 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 break;
2217 }
2218 count += sprintf(buf + count, "PM State: %s\n", state);
2219
2220 return count;
2221}
2222
2223static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2224
2225int snd_soc_dapm_sys_add(struct device *dev)
2226{
Troy Kisky12ef1932008-10-13 17:42:14 -07002227 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228}
2229
2230static void snd_soc_dapm_sys_remove(struct device *dev)
2231{
Mark Brownaef90842009-05-16 17:53:16 +01002232 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233}
2234
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002235static void dapm_free_path(struct snd_soc_dapm_path *path)
2236{
2237 list_del(&path->list_sink);
2238 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002239 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002240 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002241 kfree(path);
2242}
2243
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002245static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246{
2247 struct snd_soc_dapm_widget *w, *next_w;
2248 struct snd_soc_dapm_path *p, *next_p;
2249
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002250 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2251 if (w->dapm != dapm)
2252 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002253 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002254 /*
2255 * remove source and sink paths associated to this widget.
2256 * While removing the path, remove reference to it from both
2257 * source and sink widgets so that path is removed only once.
2258 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002259 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2260 dapm_free_path(p);
2261
2262 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2263 dapm_free_path(p);
2264
Stephen Warrenfad59882011-04-28 17:37:59 -06002265 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002266 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267 kfree(w);
2268 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269}
2270
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002271static struct snd_soc_dapm_widget *dapm_find_widget(
2272 struct snd_soc_dapm_context *dapm, const char *pin,
2273 bool search_other_contexts)
2274{
2275 struct snd_soc_dapm_widget *w;
2276 struct snd_soc_dapm_widget *fallback = NULL;
2277
2278 list_for_each_entry(w, &dapm->card->widgets, list) {
2279 if (!strcmp(w->name, pin)) {
2280 if (w->dapm == dapm)
2281 return w;
2282 else
2283 fallback = w;
2284 }
2285 }
2286
2287 if (search_other_contexts)
2288 return fallback;
2289
2290 return NULL;
2291}
2292
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002293static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002294 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002295{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002296 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002297
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002298 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002299 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002300 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002301 }
2302
Mark Brown1a8b2d92012-02-16 11:50:07 -08002303 if (w->connected != status)
2304 dapm_mark_dirty(w, "pin configuration");
2305
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002306 w->connected = status;
2307 if (status == 0)
2308 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002309
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002310 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002311}
2312
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002314 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002315 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316 *
2317 * Walks all dapm audio paths and powers widgets according to their
2318 * stream or path usage.
2319 *
2320 * Returns 0 for success.
2321 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002322int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002324 int ret;
2325
Mark Brown4f4c0072011-10-07 14:29:19 +01002326 /*
2327 * Suppress early reports (eg, jacks syncing their state) to avoid
2328 * silly DAPM runs during card startup.
2329 */
2330 if (!dapm->card || !dapm->card->instantiated)
2331 return 0;
2332
Liam Girdwood3cd04342012-03-09 12:02:08 +00002333 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002334 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002335 mutex_unlock(&dapm->card->dapm_mutex);
2336 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337}
Liam Girdwooda5302182008-07-07 13:35:17 +01002338EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002340static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2341 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2342 const char *control,
2343 int (*connected)(struct snd_soc_dapm_widget *source,
2344 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345{
2346 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002347 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348
2349 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2350 if (!path)
2351 return -ENOMEM;
2352
2353 path->source = wsource;
2354 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002355 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 INIT_LIST_HEAD(&path->list);
2357 INIT_LIST_HEAD(&path->list_source);
2358 INIT_LIST_HEAD(&path->list_sink);
2359
2360 /* check for external widgets */
2361 if (wsink->id == snd_soc_dapm_input) {
2362 if (wsource->id == snd_soc_dapm_micbias ||
2363 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002364 wsource->id == snd_soc_dapm_line ||
2365 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366 wsink->ext = 1;
2367 }
2368 if (wsource->id == snd_soc_dapm_output) {
2369 if (wsink->id == snd_soc_dapm_spk ||
2370 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002371 wsink->id == snd_soc_dapm_line ||
2372 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373 wsource->ext = 1;
2374 }
2375
2376 /* connect static paths */
2377 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002378 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379 list_add(&path->list_sink, &wsink->sources);
2380 list_add(&path->list_source, &wsource->sinks);
2381 path->connect = 1;
2382 return 0;
2383 }
2384
2385 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002386 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387 case snd_soc_dapm_adc:
2388 case snd_soc_dapm_dac:
2389 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002390 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002391 case snd_soc_dapm_input:
2392 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002393 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394 case snd_soc_dapm_micbias:
2395 case snd_soc_dapm_vmid:
2396 case snd_soc_dapm_pre:
2397 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002398 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002399 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002400 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002401 case snd_soc_dapm_aif_in:
2402 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002403 case snd_soc_dapm_dai_in:
2404 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002405 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002406 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002407 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002408 list_add(&path->list_sink, &wsink->sources);
2409 list_add(&path->list_source, &wsource->sinks);
2410 path->connect = 1;
2411 return 0;
2412 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002413 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002414 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002415 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002416 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417 if (ret != 0)
2418 goto err;
2419 break;
2420 case snd_soc_dapm_switch:
2421 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002422 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002423 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002424 if (ret != 0)
2425 goto err;
2426 break;
2427 case snd_soc_dapm_hp:
2428 case snd_soc_dapm_mic:
2429 case snd_soc_dapm_line:
2430 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002431 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432 list_add(&path->list_sink, &wsink->sources);
2433 list_add(&path->list_source, &wsource->sinks);
2434 path->connect = 0;
2435 return 0;
2436 }
Mark Brownfabd0382012-07-05 17:20:06 +01002437
2438 dapm_mark_dirty(wsource, "Route added");
2439 dapm_mark_dirty(wsink, "Route added");
2440
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002442err:
2443 kfree(path);
2444 return ret;
2445}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002447static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2448 const struct snd_soc_dapm_route *route)
2449{
2450 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2451 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2452 const char *sink;
2453 const char *source;
2454 char prefixed_sink[80];
2455 char prefixed_source[80];
2456 int ret;
2457
2458 if (dapm->codec && dapm->codec->name_prefix) {
2459 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2460 dapm->codec->name_prefix, route->sink);
2461 sink = prefixed_sink;
2462 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2463 dapm->codec->name_prefix, route->source);
2464 source = prefixed_source;
2465 } else {
2466 sink = route->sink;
2467 source = route->source;
2468 }
2469
2470 /*
2471 * find src and dest widgets over all widgets but favor a widget from
2472 * current DAPM context
2473 */
2474 list_for_each_entry(w, &dapm->card->widgets, list) {
2475 if (!wsink && !(strcmp(w->name, sink))) {
2476 wtsink = w;
2477 if (w->dapm == dapm)
2478 wsink = w;
2479 continue;
2480 }
2481 if (!wsource && !(strcmp(w->name, source))) {
2482 wtsource = w;
2483 if (w->dapm == dapm)
2484 wsource = w;
2485 }
2486 }
2487 /* use widget from another DAPM context if not found from this */
2488 if (!wsink)
2489 wsink = wtsink;
2490 if (!wsource)
2491 wsource = wtsource;
2492
2493 if (wsource == NULL) {
2494 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2495 route->source);
2496 return -ENODEV;
2497 }
2498 if (wsink == NULL) {
2499 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2500 route->sink);
2501 return -ENODEV;
2502 }
2503
2504 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2505 route->connected);
2506 if (ret)
2507 goto err;
2508
2509 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002510err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002511 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002512 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513 return ret;
2514}
Mark Brown105f1c22008-05-13 14:52:19 +02002515
Mark Brownefcc3c62012-07-05 17:24:19 +01002516static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2517 const struct snd_soc_dapm_route *route)
2518{
2519 struct snd_soc_dapm_path *path, *p;
2520 const char *sink;
2521 const char *source;
2522 char prefixed_sink[80];
2523 char prefixed_source[80];
2524
2525 if (route->control) {
2526 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002527 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002528 return -EINVAL;
2529 }
2530
2531 if (dapm->codec && dapm->codec->name_prefix) {
2532 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2533 dapm->codec->name_prefix, route->sink);
2534 sink = prefixed_sink;
2535 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2536 dapm->codec->name_prefix, route->source);
2537 source = prefixed_source;
2538 } else {
2539 sink = route->sink;
2540 source = route->source;
2541 }
2542
2543 path = NULL;
2544 list_for_each_entry(p, &dapm->card->paths, list) {
2545 if (strcmp(p->source->name, source) != 0)
2546 continue;
2547 if (strcmp(p->sink->name, sink) != 0)
2548 continue;
2549 path = p;
2550 break;
2551 }
2552
2553 if (path) {
2554 dapm_mark_dirty(path->source, "Route removed");
2555 dapm_mark_dirty(path->sink, "Route removed");
2556
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002557 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002558 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002559 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002560 source, sink);
2561 }
2562
2563 return 0;
2564}
2565
Mark Brown105f1c22008-05-13 14:52:19 +02002566/**
Mark Brown105f1c22008-05-13 14:52:19 +02002567 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002568 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002569 * @route: audio routes
2570 * @num: number of routes
2571 *
2572 * Connects 2 dapm widgets together via a named audio path. The sink is
2573 * the widget receiving the audio signal, whilst the source is the sender
2574 * of the audio signal.
2575 *
2576 * Returns 0 for success else error. On error all resources can be freed
2577 * with a call to snd_soc_card_free().
2578 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002579int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002580 const struct snd_soc_dapm_route *route, int num)
2581{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002582 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002583
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002584 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002585 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002586 r = snd_soc_dapm_add_route(dapm, route);
2587 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002588 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2589 route->source,
2590 route->control ? route->control : "direct",
2591 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002592 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002593 }
2594 route++;
2595 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002596 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002597
Dan Carpenter60884c22012-04-13 22:25:43 +03002598 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002599}
2600EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2601
Mark Brownefcc3c62012-07-05 17:24:19 +01002602/**
2603 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2604 * @dapm: DAPM context
2605 * @route: audio routes
2606 * @num: number of routes
2607 *
2608 * Removes routes from the DAPM context.
2609 */
2610int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2611 const struct snd_soc_dapm_route *route, int num)
2612{
2613 int i, ret = 0;
2614
2615 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2616 for (i = 0; i < num; i++) {
2617 snd_soc_dapm_del_route(dapm, route);
2618 route++;
2619 }
2620 mutex_unlock(&dapm->card->dapm_mutex);
2621
2622 return ret;
2623}
2624EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2625
Mark Brownbf3a9e12011-06-13 16:42:29 +01002626static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2627 const struct snd_soc_dapm_route *route)
2628{
2629 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2630 route->source,
2631 true);
2632 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2633 route->sink,
2634 true);
2635 struct snd_soc_dapm_path *path;
2636 int count = 0;
2637
2638 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002639 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002640 route->source);
2641 return -ENODEV;
2642 }
2643
2644 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002645 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002646 route->sink);
2647 return -ENODEV;
2648 }
2649
2650 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002651 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002652 route->source, route->sink);
2653
2654 list_for_each_entry(path, &source->sinks, list_source) {
2655 if (path->sink == sink) {
2656 path->weak = 1;
2657 count++;
2658 }
2659 }
2660
2661 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002662 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002663 route->source, route->sink);
2664 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002665 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002666 count, route->source, route->sink);
2667
2668 return 0;
2669}
2670
2671/**
2672 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2673 * @dapm: DAPM context
2674 * @route: audio routes
2675 * @num: number of routes
2676 *
2677 * Mark existing routes matching those specified in the passed array
2678 * as being weak, meaning that they are ignored for the purpose of
2679 * power decisions. The main intended use case is for sidetone paths
2680 * which couple audio between other independent paths if they are both
2681 * active in order to make the combination work better at the user
2682 * level but which aren't intended to be "used".
2683 *
2684 * Note that CODEC drivers should not use this as sidetone type paths
2685 * can frequently also be used as bypass paths.
2686 */
2687int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2688 const struct snd_soc_dapm_route *route, int num)
2689{
2690 int i, err;
2691 int ret = 0;
2692
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002693 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002694 for (i = 0; i < num; i++) {
2695 err = snd_soc_dapm_weak_route(dapm, route);
2696 if (err)
2697 ret = err;
2698 route++;
2699 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002700 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002701
2702 return ret;
2703}
2704EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2705
Mark Brown105f1c22008-05-13 14:52:19 +02002706/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002708 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709 *
2710 * Checks the codec for any new dapm widgets and creates them if found.
2711 *
2712 * Returns 0 for success.
2713 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002714int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002716 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002718 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002719
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002720 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002721
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002722 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002723 {
2724 if (w->new)
2725 continue;
2726
Stephen Warrenfad59882011-04-28 17:37:59 -06002727 if (w->num_kcontrols) {
2728 w->kcontrols = kzalloc(w->num_kcontrols *
2729 sizeof(struct snd_kcontrol *),
2730 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002731 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002732 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002733 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002734 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002735 }
2736
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 switch(w->id) {
2738 case snd_soc_dapm_switch:
2739 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002740 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002741 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002742 break;
2743 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002744 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002745 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002746 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002749 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002750 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002752 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753 break;
2754 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002755
2756 /* Read the initial power state from the device */
2757 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002758 val = soc_widget_read(w, w->reg) >> w->shift;
2759 val &= w->mask;
2760 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002761 w->power = 1;
2762 }
2763
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002765
Mark Brown7508b122011-10-05 12:09:12 +01002766 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002767 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768 }
2769
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002770 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2771 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002772 return 0;
2773}
2774EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2775
2776/**
2777 * snd_soc_dapm_get_volsw - dapm mixer get callback
2778 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002779 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 *
2781 * Callback to get the value of a dapm mixer control.
2782 *
2783 * Returns 0 for success.
2784 */
2785int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
2787{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002788 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002789 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002790 struct soc_mixer_control *mc =
2791 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002792 unsigned int reg = mc->reg;
2793 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002794 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002795 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002796 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002797 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002798
2799 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002800 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002801 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002802 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002803
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002804 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2805 if (dapm_kcontrol_is_powered(kcontrol))
2806 val = (snd_soc_read(codec, reg) >> shift) & mask;
2807 else
2808 val = dapm_kcontrol_get_value(kcontrol);
2809 mutex_unlock(&card->dapm_mutex);
2810
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002811 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002812 ucontrol->value.integer.value[0] = max - val;
2813 else
2814 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002815
2816 return 0;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2819
2820/**
2821 * snd_soc_dapm_put_volsw - dapm mixer set callback
2822 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002823 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824 *
2825 * Callback to set the value of a dapm mixer control.
2826 *
2827 * Returns 0 for success.
2828 */
2829int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002832 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002833 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002834 struct soc_mixer_control *mc =
2835 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002836 unsigned int reg = mc->reg;
2837 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002838 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002839 unsigned int mask = (1 << fls(max)) - 1;
2840 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002841 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002842 int connect, change;
2843 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002844
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002845 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002846 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002847 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002848 kcontrol->id.name);
2849
Richard Purdie2b97eab2006-10-06 18:32:18 +02002850 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002851 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002852
2853 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002854 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855
Liam Girdwood3cd04342012-03-09 12:02:08 +00002856 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002857
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002858 dapm_kcontrol_set_value(kcontrol, val);
2859
2860 mask = mask << shift;
2861 val = val << shift;
2862
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002863 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002864 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002865 update.kcontrol = kcontrol;
2866 update.reg = reg;
2867 update.mask = mask;
2868 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002869
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002870 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002871
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002872 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002873
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002874 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002875 }
2876
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002877 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002878 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002879}
2880EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2881
2882/**
2883 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2884 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002885 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002886 *
2887 * Callback to get the value of a dapm enumerated double mixer control.
2888 *
2889 * Returns 0 for success.
2890 */
2891int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2893{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002894 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002895 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002896 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002897
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002898 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002899 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002900 if (e->shift_l != e->shift_r)
2901 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002902 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903
2904 return 0;
2905}
2906EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2907
2908/**
2909 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2910 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002911 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002912 *
2913 * Callback to set the value of a dapm enumerated double mixer control.
2914 *
2915 * Returns 0 for success.
2916 */
2917int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2918 struct snd_ctl_elem_value *ucontrol)
2919{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002920 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002921 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002922 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002923 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002924 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002925 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002926
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002927 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002928 return -EINVAL;
2929 mux = ucontrol->value.enumerated.item[0];
2930 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002931 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002932 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002933 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002934 return -EINVAL;
2935 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002936 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937 }
2938
Liam Girdwood3cd04342012-03-09 12:02:08 +00002939 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002940
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002941 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002942 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002943 update.kcontrol = kcontrol;
2944 update.reg = e->reg;
2945 update.mask = mask;
2946 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002947 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002948
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002949 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002950
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002951 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002952 }
2953
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002954 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002955 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956}
2957EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2958
2959/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002960 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2961 * @kcontrol: mixer control
2962 * @ucontrol: control element information
2963 *
2964 * Returns 0 for success.
2965 */
2966int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2968{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002969 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002970 return 0;
2971}
2972EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2973
2974/**
2975 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2976 * @kcontrol: mixer control
2977 * @ucontrol: control element information
2978 *
2979 * Returns 0 for success.
2980 */
2981int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_value *ucontrol)
2983{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002984 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002985 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002986 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002987 struct soc_enum *e =
2988 (struct soc_enum *)kcontrol->private_value;
2989 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002990
2991 if (ucontrol->value.enumerated.item[0] >= e->max)
2992 return -EINVAL;
2993
Liam Girdwood3cd04342012-03-09 12:02:08 +00002994 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002995
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002996 value = ucontrol->value.enumerated.item[0];
2997 change = dapm_kcontrol_set_value(kcontrol, value);
2998 if (change)
2999 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003000
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003001 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003002 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3005
3006/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003007 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3008 * callback
3009 * @kcontrol: mixer control
3010 * @ucontrol: control element information
3011 *
3012 * Callback to get the value of a dapm semi enumerated double mixer control.
3013 *
3014 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3015 * used for handling bitfield coded enumeration for example.
3016 *
3017 * Returns 0 for success.
3018 */
3019int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3020 struct snd_ctl_elem_value *ucontrol)
3021{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003022 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003023 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003024 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003025
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003026 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003027 val = (reg_val >> e->shift_l) & e->mask;
3028 for (mux = 0; mux < e->max; mux++) {
3029 if (val == e->values[mux])
3030 break;
3031 }
3032 ucontrol->value.enumerated.item[0] = mux;
3033 if (e->shift_l != e->shift_r) {
3034 val = (reg_val >> e->shift_r) & e->mask;
3035 for (mux = 0; mux < e->max; mux++) {
3036 if (val == e->values[mux])
3037 break;
3038 }
3039 ucontrol->value.enumerated.item[1] = mux;
3040 }
3041
3042 return 0;
3043}
3044EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3045
3046/**
3047 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3048 * callback
3049 * @kcontrol: mixer control
3050 * @ucontrol: control element information
3051 *
3052 * Callback to set the value of a dapm semi enumerated double mixer control.
3053 *
3054 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3055 * used for handling bitfield coded enumeration for example.
3056 *
3057 * Returns 0 for success.
3058 */
3059int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3060 struct snd_ctl_elem_value *ucontrol)
3061{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003062 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003063 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003064 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003065 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003066 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003067 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003068
3069 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3070 return -EINVAL;
3071 mux = ucontrol->value.enumerated.item[0];
3072 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3073 mask = e->mask << e->shift_l;
3074 if (e->shift_l != e->shift_r) {
3075 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3076 return -EINVAL;
3077 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3078 mask |= e->mask << e->shift_r;
3079 }
3080
Liam Girdwood3cd04342012-03-09 12:02:08 +00003081 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003082
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003083 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003084 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003085 update.kcontrol = kcontrol;
3086 update.reg = e->reg;
3087 update.mask = mask;
3088 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003089 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003090
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003091 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003092
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003093 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003094 }
3095
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003096 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003097 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003098}
3099EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3100
3101/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003102 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3103 *
3104 * @kcontrol: mixer control
3105 * @uinfo: control element information
3106 *
3107 * Callback to provide information about a pin switch control.
3108 */
3109int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3110 struct snd_ctl_elem_info *uinfo)
3111{
3112 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3113 uinfo->count = 1;
3114 uinfo->value.integer.min = 0;
3115 uinfo->value.integer.max = 1;
3116
3117 return 0;
3118}
3119EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3120
3121/**
3122 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3123 *
3124 * @kcontrol: mixer control
3125 * @ucontrol: Value
3126 */
3127int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3128 struct snd_ctl_elem_value *ucontrol)
3129{
Mark Brown48a8c392012-02-14 17:11:15 -08003130 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003131 const char *pin = (const char *)kcontrol->private_value;
3132
Liam Girdwood3cd04342012-03-09 12:02:08 +00003133 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003134
3135 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003136 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003137
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003138 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003139
3140 return 0;
3141}
3142EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3143
3144/**
3145 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3146 *
3147 * @kcontrol: mixer control
3148 * @ucontrol: Value
3149 */
3150int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
3152{
Mark Brown48a8c392012-02-14 17:11:15 -08003153 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003154 const char *pin = (const char *)kcontrol->private_value;
3155
Liam Girdwood3cd04342012-03-09 12:02:08 +00003156 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003157
3158 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003159 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003160 else
Mark Brown48a8c392012-02-14 17:11:15 -08003161 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003162
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003163 mutex_unlock(&card->dapm_mutex);
3164
Mark Brown48a8c392012-02-14 17:11:15 -08003165 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003166 return 0;
3167}
3168EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3169
Mark Brown5ba06fc2012-02-16 11:07:13 -08003170static struct snd_soc_dapm_widget *
3171snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3172 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003173{
3174 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003175 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003176
3177 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003178 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179
Mark Brown62ea8742012-01-21 21:14:48 +00003180 switch (w->id) {
3181 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003182 w->regulator = devm_regulator_get(dapm->dev, w->name);
3183 if (IS_ERR(w->regulator)) {
3184 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003185 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003186 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003187 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003188 }
Mark Brown8784c772013-01-10 19:33:47 +00003189
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003190 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003191 ret = regulator_allow_bypass(w->regulator, true);
3192 if (ret != 0)
3193 dev_warn(w->dapm->dev,
3194 "ASoC: Failed to unbypass %s: %d\n",
3195 w->name, ret);
3196 }
Mark Brown62ea8742012-01-21 21:14:48 +00003197 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003198 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003199#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003200 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003201 if (IS_ERR(w->clk)) {
3202 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003203 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003204 w->name, ret);
3205 return NULL;
3206 }
Mark Brownec029952012-06-04 08:16:20 +01003207#else
3208 return NULL;
3209#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003210 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003211 default:
3212 break;
3213 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003214
Mark Brown88e8b9a2011-03-02 18:18:24 +00003215 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003216 w->name = kasprintf(GFP_KERNEL, "%s %s",
3217 dapm->codec->name_prefix, widget->name);
3218 else
3219 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3220
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003221 if (w->name == NULL) {
3222 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003223 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003224 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003225
Mark Brown7ca3a182011-10-08 14:04:50 +01003226 switch (w->id) {
3227 case snd_soc_dapm_switch:
3228 case snd_soc_dapm_mixer:
3229 case snd_soc_dapm_mixer_named_ctl:
3230 w->power_check = dapm_generic_check_power;
3231 break;
3232 case snd_soc_dapm_mux:
3233 case snd_soc_dapm_virt_mux:
3234 case snd_soc_dapm_value_mux:
3235 w->power_check = dapm_generic_check_power;
3236 break;
Mark Brown46162742013-06-05 19:36:11 +01003237 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003238 w->power_check = dapm_adc_check_power;
3239 break;
Mark Brown46162742013-06-05 19:36:11 +01003240 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003241 w->power_check = dapm_dac_check_power;
3242 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003243 case snd_soc_dapm_adc:
3244 case snd_soc_dapm_aif_out:
3245 case snd_soc_dapm_dac:
3246 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003247 case snd_soc_dapm_pga:
3248 case snd_soc_dapm_out_drv:
3249 case snd_soc_dapm_input:
3250 case snd_soc_dapm_output:
3251 case snd_soc_dapm_micbias:
3252 case snd_soc_dapm_spk:
3253 case snd_soc_dapm_hp:
3254 case snd_soc_dapm_mic:
3255 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003256 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003257 w->power_check = dapm_generic_check_power;
3258 break;
3259 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003260 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003261 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003262 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003263 w->power_check = dapm_supply_check_power;
3264 break;
3265 default:
3266 w->power_check = dapm_always_on_check_power;
3267 break;
3268 }
3269
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003270 w->dapm = dapm;
3271 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003272 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003273 INIT_LIST_HEAD(&w->sources);
3274 INIT_LIST_HEAD(&w->sinks);
3275 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003276 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003277 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003278
3279 /* machine layer set ups unconnected pins and insertions */
3280 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003281 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003282}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003283
3284/**
Mark Brown4ba13272008-05-13 14:51:19 +02003285 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003286 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003287 * @widget: widget array
3288 * @num: number of widgets
3289 *
3290 * Creates new DAPM controls based upon the templates.
3291 *
3292 * Returns 0 for success else error.
3293 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003294int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003295 const struct snd_soc_dapm_widget *widget,
3296 int num)
3297{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003298 struct snd_soc_dapm_widget *w;
3299 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003300 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003301
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003302 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003303 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003304 w = snd_soc_dapm_new_control(dapm, widget);
3305 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003306 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003307 "ASoC: Failed to create DAPM control %s\n",
3308 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003309 ret = -ENOMEM;
3310 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003311 }
Mark Brown4ba13272008-05-13 14:51:19 +02003312 widget++;
3313 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003314 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003315 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003316}
3317EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3318
Mark Brownc74184e2012-04-04 22:12:09 +01003319static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3320 struct snd_kcontrol *kcontrol, int event)
3321{
3322 struct snd_soc_dapm_path *source_p, *sink_p;
3323 struct snd_soc_dai *source, *sink;
3324 const struct snd_soc_pcm_stream *config = w->params;
3325 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003326 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003327 u64 fmt;
3328 int ret;
3329
3330 BUG_ON(!config);
3331 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3332
3333 /* We only support a single source and sink, pick the first */
3334 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3335 list_sink);
3336 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3337 list_source);
3338
3339 BUG_ON(!source_p || !sink_p);
3340 BUG_ON(!sink_p->source || !source_p->sink);
3341 BUG_ON(!source_p->source || !sink_p->sink);
3342
3343 source = source_p->source->priv;
3344 sink = sink_p->sink->priv;
3345
3346 /* Be a little careful as we don't want to overflow the mask array */
3347 if (config->formats) {
3348 fmt = ffs(config->formats) - 1;
3349 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003350 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003351 config->formats);
3352 fmt = 0;
3353 }
3354
3355 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003356 params = kzalloc(sizeof(*params), GFP_KERNEL);
3357 if (!params) {
3358 ret = -ENOMEM;
3359 goto out;
3360 }
3361 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003362
Mark Brown9747cec2012-04-26 19:12:21 +01003363 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003364 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003365 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003366 config->rate_max;
3367
Mark Brown9747cec2012-04-26 19:12:21 +01003368 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003369 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003370 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003371 = config->channels_max;
3372
3373 memset(&substream, 0, sizeof(substream));
3374
3375 switch (event) {
3376 case SND_SOC_DAPM_PRE_PMU:
3377 if (source->driver->ops && source->driver->ops->hw_params) {
3378 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3379 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003380 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003381 if (ret != 0) {
3382 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003383 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003384 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003385 }
3386 }
3387
3388 if (sink->driver->ops && sink->driver->ops->hw_params) {
3389 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003390 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003391 sink);
3392 if (ret != 0) {
3393 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003394 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003395 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003396 }
3397 }
3398 break;
3399
3400 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003401 ret = snd_soc_dai_digital_mute(sink, 0,
3402 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003403 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003404 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003405 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003406 break;
3407
3408 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003409 ret = snd_soc_dai_digital_mute(sink, 1,
3410 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003411 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003412 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003413 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003414 break;
3415
3416 default:
3417 BUG();
3418 return -EINVAL;
3419 }
3420
Mark Brown9747cec2012-04-26 19:12:21 +01003421out:
3422 kfree(params);
3423 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003424}
3425
3426int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3427 const struct snd_soc_pcm_stream *params,
3428 struct snd_soc_dapm_widget *source,
3429 struct snd_soc_dapm_widget *sink)
3430{
3431 struct snd_soc_dapm_route routes[2];
3432 struct snd_soc_dapm_widget template;
3433 struct snd_soc_dapm_widget *w;
3434 size_t len;
3435 char *link_name;
3436
3437 len = strlen(source->name) + strlen(sink->name) + 2;
3438 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3439 if (!link_name)
3440 return -ENOMEM;
3441 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3442
3443 memset(&template, 0, sizeof(template));
3444 template.reg = SND_SOC_NOPM;
3445 template.id = snd_soc_dapm_dai_link;
3446 template.name = link_name;
3447 template.event = snd_soc_dai_link_event;
3448 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3449 SND_SOC_DAPM_PRE_PMD;
3450
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003451 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003452
3453 w = snd_soc_dapm_new_control(&card->dapm, &template);
3454 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003455 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003456 link_name);
3457 return -ENOMEM;
3458 }
3459
3460 w->params = params;
3461
3462 memset(&routes, 0, sizeof(routes));
3463
3464 routes[0].source = source->name;
3465 routes[0].sink = link_name;
3466 routes[1].source = link_name;
3467 routes[1].sink = sink->name;
3468
3469 return snd_soc_dapm_add_routes(&card->dapm, routes,
3470 ARRAY_SIZE(routes));
3471}
3472
Mark Brown888df392012-02-16 19:37:51 -08003473int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3474 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003475{
Mark Brown888df392012-02-16 19:37:51 -08003476 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003477 struct snd_soc_dapm_widget *w;
3478
Mark Brown888df392012-02-16 19:37:51 -08003479 WARN_ON(dapm->dev != dai->dev);
3480
3481 memset(&template, 0, sizeof(template));
3482 template.reg = SND_SOC_NOPM;
3483
3484 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003485 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003486 template.name = dai->driver->playback.stream_name;
3487 template.sname = dai->driver->playback.stream_name;
3488
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003489 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003490 template.name);
3491
3492 w = snd_soc_dapm_new_control(dapm, &template);
3493 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003494 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003495 dai->driver->playback.stream_name);
3496 }
3497
3498 w->priv = dai;
3499 dai->playback_widget = w;
3500 }
3501
3502 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003503 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003504 template.name = dai->driver->capture.stream_name;
3505 template.sname = dai->driver->capture.stream_name;
3506
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003507 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003508 template.name);
3509
3510 w = snd_soc_dapm_new_control(dapm, &template);
3511 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003512 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003513 dai->driver->capture.stream_name);
3514 }
3515
3516 w->priv = dai;
3517 dai->capture_widget = w;
3518 }
3519
3520 return 0;
3521}
3522
3523int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3524{
3525 struct snd_soc_dapm_widget *dai_w, *w;
3526 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003527
3528 /* For each DAI widget... */
3529 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003530 switch (dai_w->id) {
3531 case snd_soc_dapm_dai_in:
3532 case snd_soc_dapm_dai_out:
3533 break;
3534 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003535 continue;
Mark Brown46162742013-06-05 19:36:11 +01003536 }
Mark Brown888df392012-02-16 19:37:51 -08003537
3538 dai = dai_w->priv;
3539
3540 /* ...find all widgets with the same stream and link them */
3541 list_for_each_entry(w, &card->widgets, list) {
3542 if (w->dapm != dai_w->dapm)
3543 continue;
3544
Mark Brown46162742013-06-05 19:36:11 +01003545 switch (w->id) {
3546 case snd_soc_dapm_dai_in:
3547 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003548 continue;
Mark Brown46162742013-06-05 19:36:11 +01003549 default:
3550 break;
3551 }
Mark Brown888df392012-02-16 19:37:51 -08003552
Russell King19c2c5f2013-08-04 20:24:03 +01003553 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003554 continue;
3555
3556 if (dai->driver->playback.stream_name &&
3557 strstr(w->sname,
3558 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003559 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003560 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003561
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003562 snd_soc_dapm_add_path(w->dapm,
3563 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003564 }
3565
3566 if (dai->driver->capture.stream_name &&
3567 strstr(w->sname,
3568 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003569 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003570 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003571
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003572 snd_soc_dapm_add_path(w->dapm, w,
3573 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003574 }
3575 }
3576 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003577
Mark Brown888df392012-02-16 19:37:51 -08003578 return 0;
3579}
Liam Girdwood64a648c222011-07-25 11:15:15 +01003580
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003581static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3582 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003583{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003584
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003585 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3586 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3587 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003588
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003589 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3590 w_cpu = cpu_dai->playback_widget;
3591 w_codec = codec_dai->playback_widget;
3592 } else {
3593 w_cpu = cpu_dai->capture_widget;
3594 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003595 }
3596
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003597 if (w_cpu) {
3598
3599 dapm_mark_dirty(w_cpu, "stream event");
3600
3601 switch (event) {
3602 case SND_SOC_DAPM_STREAM_START:
3603 w_cpu->active = 1;
3604 break;
3605 case SND_SOC_DAPM_STREAM_STOP:
3606 w_cpu->active = 0;
3607 break;
3608 case SND_SOC_DAPM_STREAM_SUSPEND:
3609 case SND_SOC_DAPM_STREAM_RESUME:
3610 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3611 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3612 break;
3613 }
3614 }
3615
3616 if (w_codec) {
3617
3618 dapm_mark_dirty(w_codec, "stream event");
3619
3620 switch (event) {
3621 case SND_SOC_DAPM_STREAM_START:
3622 w_codec->active = 1;
3623 break;
3624 case SND_SOC_DAPM_STREAM_STOP:
3625 w_codec->active = 0;
3626 break;
3627 case SND_SOC_DAPM_STREAM_SUSPEND:
3628 case SND_SOC_DAPM_STREAM_RESUME:
3629 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3630 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3631 break;
3632 }
3633 }
3634
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003635 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003636}
3637
3638/**
3639 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3640 * @rtd: PCM runtime data
3641 * @stream: stream name
3642 * @event: stream event
3643 *
3644 * Sends a stream event to the dapm core. The core then makes any
3645 * necessary widget power changes.
3646 *
3647 * Returns 0 for success else error.
3648 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003649void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3650 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003651{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003652 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003653
Liam Girdwood3cd04342012-03-09 12:02:08 +00003654 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003655 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003656 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003657}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003658
3659/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003660 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003661 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003662 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003663 *
Mark Brown74b8f952009-06-06 11:26:15 +01003664 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003665 * a valid audio route and active audio stream.
3666 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3667 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003668 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003669int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003670{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003671 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003672}
Liam Girdwooda5302182008-07-07 13:35:17 +01003673EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003674
3675/**
Mark Brownda341832010-03-15 19:23:37 +00003676 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003677 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003678 * @pin: pin name
3679 *
3680 * Enables input/output pin regardless of any other state. This is
3681 * intended for use with microphone bias supplies used in microphone
3682 * jack detection.
3683 *
3684 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3685 * do any widget power switching.
3686 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003687int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3688 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003689{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003690 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003691
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003692 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003693 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003694 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003695 }
3696
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003697 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003698 w->connected = 1;
3699 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003700 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003701
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003702 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003703}
3704EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3705
3706/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003707 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003708 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003709 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003710 *
Mark Brown74b8f952009-06-06 11:26:15 +01003711 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003712 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3713 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003714 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3716 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003717{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003718 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003719}
3720EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3721
3722/**
Mark Brown5817b522008-09-24 11:23:11 +01003723 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003724 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003725 * @pin: pin name
3726 *
3727 * Marks the specified pin as being not connected, disabling it along
3728 * any parent or child widgets. At present this is identical to
3729 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3730 * additional things such as disabling controls which only affect
3731 * paths through the pin.
3732 *
3733 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3734 * do any widget power switching.
3735 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003736int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003737{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003738 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003739}
3740EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3741
3742/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003743 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003745 * @pin: audio signal pin endpoint (or start point)
3746 *
3747 * Get audio pin status - connected or disconnected.
3748 *
3749 * Returns 1 for connected otherwise 0.
3750 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003751int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3752 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003753{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003754 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003755
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003756 if (w)
3757 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003758
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003759 return 0;
3760}
Liam Girdwooda5302182008-07-07 13:35:17 +01003761EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003762
3763/**
Mark Brown1547aba2010-05-07 21:11:40 +01003764 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003766 * @pin: audio signal pin endpoint (or start point)
3767 *
3768 * Mark the given endpoint or pin as ignoring suspend. When the
3769 * system is disabled a path between two endpoints flagged as ignoring
3770 * suspend will not be disabled. The path must already be enabled via
3771 * normal means at suspend time, it will not be turned on if it was not
3772 * already enabled.
3773 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003774int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3775 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003776{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003777 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003778
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003779 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003780 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003781 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003782 }
3783
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003784 w->ignore_suspend = 1;
3785
3786 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003787}
3788EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3789
Stephen Warren16332812011-11-23 12:42:04 -07003790static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3791 struct snd_soc_dapm_widget *w)
3792{
3793 struct snd_soc_dapm_path *p;
3794
3795 list_for_each_entry(p, &card->paths, list) {
3796 if ((p->source == w) || (p->sink == w)) {
3797 dev_dbg(card->dev,
3798 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3799 p->source->name, p->source->id, p->source->dapm,
3800 p->sink->name, p->sink->id, p->sink->dapm);
3801
3802 /* Connected to something other than the codec */
3803 if (p->source->dapm != p->sink->dapm)
3804 return true;
3805 /*
3806 * Loopback connection from codec external pin to
3807 * codec external pin
3808 */
3809 if (p->sink->id == snd_soc_dapm_input) {
3810 switch (p->source->id) {
3811 case snd_soc_dapm_output:
3812 case snd_soc_dapm_micbias:
3813 return true;
3814 default:
3815 break;
3816 }
3817 }
3818 }
3819 }
3820
3821 return false;
3822}
3823
3824/**
3825 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3826 * @codec: The codec whose pins should be processed
3827 *
3828 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3829 * which are unused. Pins are used if they are connected externally to the
3830 * codec, whether that be to some other device, or a loop-back connection to
3831 * the codec itself.
3832 */
3833void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3834{
3835 struct snd_soc_card *card = codec->card;
3836 struct snd_soc_dapm_context *dapm = &codec->dapm;
3837 struct snd_soc_dapm_widget *w;
3838
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003839 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003840 &card->dapm, &codec->dapm);
3841
3842 list_for_each_entry(w, &card->widgets, list) {
3843 if (w->dapm != dapm)
3844 continue;
3845 switch (w->id) {
3846 case snd_soc_dapm_input:
3847 case snd_soc_dapm_output:
3848 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003849 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003850 w->name);
3851 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003852 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003853 "... Not in map; disabling\n");
3854 snd_soc_dapm_nc_pin(dapm, w->name);
3855 }
3856 break;
3857 default:
3858 break;
3859 }
3860 }
3861}
3862
Mark Brown1547aba2010-05-07 21:11:40 +01003863/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003864 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003865 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003866 *
3867 * Free all dapm widgets and resources.
3868 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003869void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003870{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003871 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003872 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003873 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003874 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003875}
3876EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3877
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003878static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003879{
Liam Girdwood01005a72012-07-06 16:57:05 +01003880 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003881 struct snd_soc_dapm_widget *w;
3882 LIST_HEAD(down_list);
3883 int powerdown = 0;
3884
Liam Girdwood01005a72012-07-06 16:57:05 +01003885 mutex_lock(&card->dapm_mutex);
3886
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003887 list_for_each_entry(w, &dapm->card->widgets, list) {
3888 if (w->dapm != dapm)
3889 continue;
Mark Brown51737472009-06-22 13:16:51 +01003890 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003891 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003892 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003893 powerdown = 1;
3894 }
3895 }
3896
3897 /* If there were no widgets to power down we're already in
3898 * standby.
3899 */
3900 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003901 if (dapm->bias_level == SND_SOC_BIAS_ON)
3902 snd_soc_dapm_set_bias_level(dapm,
3903 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003904 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003905 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3906 snd_soc_dapm_set_bias_level(dapm,
3907 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003908 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003909
3910 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003911}
Mark Brown51737472009-06-22 13:16:51 +01003912
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913/*
3914 * snd_soc_dapm_shutdown - callback for system shutdown
3915 */
3916void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3917{
3918 struct snd_soc_codec *codec;
3919
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003920 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003921 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003922 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3923 snd_soc_dapm_set_bias_level(&codec->dapm,
3924 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003925 }
Mark Brown51737472009-06-22 13:16:51 +01003926}
3927
Richard Purdie2b97eab2006-10-06 18:32:18 +02003928/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003929MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003930MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3931MODULE_LICENSE("GPL");