blob: 0944bc4bd4a49faf634b76406330f405b7dff573 [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,
668 int kci, struct snd_soc_dapm_path *path)
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;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200769 dapm_kcontrol_add_path(kcontrol, path);
Stephen Warren85762e72013-03-29 15:40:10 -0600770
771 return 0;
772}
773
774/* create new dapm mixer control */
775static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
776{
777 int i, ret;
778 struct snd_soc_dapm_path *path;
779
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780 /* add kcontrol */
781 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782 /* match name */
783 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200784 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600785 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200786 continue;
787
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200788 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200789 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200790 continue;
791 }
792
Stephen Warren85762e72013-03-29 15:40:10 -0600793 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
794 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200795 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796 }
797 }
Stephen Warren85762e72013-03-29 15:40:10 -0600798
799 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800}
801
802/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200803static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200804{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200805 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600806 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600807 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808
Stephen Warrenaf468002011-04-28 17:38:01 -0600809 if (w->num_kcontrols != 1) {
810 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000811 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600812 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813 return -EINVAL;
814 }
815
Stephen Warren85762e72013-03-29 15:40:10 -0600816 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
817 list_sink);
818 if (!path) {
819 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
820 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600821 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200822
Stephen Warren85762e72013-03-29 15:40:10 -0600823 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
824 if (ret < 0)
825 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600826
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200828 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200829
Stephen Warrenaf468002011-04-28 17:38:01 -0600830 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831}
832
833/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200834static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200835{
Mark Browna6c65732010-03-03 17:45:21 +0000836 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200837 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000838 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839
Mark Browna6c65732010-03-03 17:45:21 +0000840 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200841}
842
843/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100844static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
845 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200846{
847 struct snd_soc_dapm_path *p;
848
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100849 list_for_each_entry(p, sink, list_source) {
850 if (p->walked) {
851 p->walked = 0;
852 dapm_clear_walk_output(dapm, &p->sink->sinks);
853 }
854 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200855}
856
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100857static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
858 struct list_head *source)
859{
860 struct snd_soc_dapm_path *p;
861
862 list_for_each_entry(p, source, list_sink) {
863 if (p->walked) {
864 p->walked = 0;
865 dapm_clear_walk_input(dapm, &p->source->sources);
866 }
867 }
868}
869
870
Mark Brown99497882010-05-07 20:24:05 +0100871/* We implement power down on suspend by checking the power state of
872 * the ALSA card - when we are suspending the ALSA state for the card
873 * is set to D3.
874 */
875static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
876{
Mark Brown12ea2c72011-03-02 18:17:32 +0000877 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100880 case SNDRV_CTL_POWER_D3hot:
881 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100882 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000883 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200884 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100885 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100886 default:
887 return 1;
888 }
889}
890
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100891/* add widget to list if it's not already in the list */
892static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
893 struct snd_soc_dapm_widget *w)
894{
895 struct snd_soc_dapm_widget_list *wlist;
896 int wlistsize, wlistentries, i;
897
898 if (*list == NULL)
899 return -EINVAL;
900
901 wlist = *list;
902
903 /* is this widget already in the list */
904 for (i = 0; i < wlist->num_widgets; i++) {
905 if (wlist->widgets[i] == w)
906 return 0;
907 }
908
909 /* allocate some new space */
910 wlistentries = wlist->num_widgets + 1;
911 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
912 wlistentries * sizeof(struct snd_soc_dapm_widget *);
913 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
914 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000915 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100916 w->name);
917 return -ENOMEM;
918 }
919 wlist = *list;
920
921 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000922 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100923 w->name, wlist->num_widgets);
924
925 wlist->widgets[wlist->num_widgets] = w;
926 wlist->num_widgets++;
927 return 1;
928}
929
Richard Purdie2b97eab2006-10-06 18:32:18 +0200930/*
931 * Recursively check for a completed path to an active or physically connected
932 * output widget. Returns number of complete paths.
933 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100934static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
935 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936{
937 struct snd_soc_dapm_path *path;
938 int con = 0;
939
Mark Brown024dc072011-10-09 11:52:05 +0100940 if (widget->outputs >= 0)
941 return widget->outputs;
942
Mark Brownde02d072011-09-20 21:43:24 +0100943 DAPM_UPDATE_STAT(widget, path_checks);
944
Mark Brown62ea8742012-01-21 21:14:48 +0000945 switch (widget->id) {
946 case snd_soc_dapm_supply:
947 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200948 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200949 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100950 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000951 default:
952 break;
953 }
Mark Brown246d0a12009-04-22 18:24:55 +0100954
Mark Brown010ff262009-08-17 17:39:22 +0100955 switch (widget->id) {
956 case snd_soc_dapm_adc:
957 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100958 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100959 if (widget->active) {
960 widget->outputs = snd_soc_dapm_suspend_check(widget);
961 return widget->outputs;
962 }
Mark Brown010ff262009-08-17 17:39:22 +0100963 default:
964 break;
965 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966
967 if (widget->connected) {
968 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100969 if (widget->id == snd_soc_dapm_output && !widget->ext) {
970 widget->outputs = snd_soc_dapm_suspend_check(widget);
971 return widget->outputs;
972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973
974 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100975 if (widget->id == snd_soc_dapm_hp ||
976 widget->id == snd_soc_dapm_spk ||
977 (widget->id == snd_soc_dapm_line &&
978 !list_empty(&widget->sources))) {
979 widget->outputs = snd_soc_dapm_suspend_check(widget);
980 return widget->outputs;
981 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982 }
983
984 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100985 DAPM_UPDATE_STAT(widget, neighbour_checks);
986
Mark Brownbf3a9e12011-06-13 16:42:29 +0100987 if (path->weak)
988 continue;
989
Mark Brown8af294b2013-02-22 17:48:15 +0000990 if (path->walking)
991 return 1;
992
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993 if (path->walked)
994 continue;
995
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100996 trace_snd_soc_dapm_output_path(widget, path);
997
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998 if (path->sink && path->connect) {
999 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001000 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001001
1002 /* do we need to add this widget to the list ? */
1003 if (list) {
1004 int err;
1005 err = dapm_list_add_widget(list, path->sink);
1006 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001007 dev_err(widget->dapm->dev,
1008 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001009 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001010 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001011 return con;
1012 }
1013 }
1014
1015 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001016
1017 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018 }
1019 }
1020
Mark Brown024dc072011-10-09 11:52:05 +01001021 widget->outputs = con;
1022
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 return con;
1024}
1025
1026/*
1027 * Recursively check for a completed path to an active or physically connected
1028 * input widget. Returns number of complete paths.
1029 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001030static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1031 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001032{
1033 struct snd_soc_dapm_path *path;
1034 int con = 0;
1035
Mark Brown024dc072011-10-09 11:52:05 +01001036 if (widget->inputs >= 0)
1037 return widget->inputs;
1038
Mark Brownde02d072011-09-20 21:43:24 +01001039 DAPM_UPDATE_STAT(widget, path_checks);
1040
Mark Brown62ea8742012-01-21 21:14:48 +00001041 switch (widget->id) {
1042 case snd_soc_dapm_supply:
1043 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001044 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001045 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001046 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001047 default:
1048 break;
1049 }
Mark Brown246d0a12009-04-22 18:24:55 +01001050
Richard Purdie2b97eab2006-10-06 18:32:18 +02001051 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001052 switch (widget->id) {
1053 case snd_soc_dapm_dac:
1054 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001055 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001056 if (widget->active) {
1057 widget->inputs = snd_soc_dapm_suspend_check(widget);
1058 return widget->inputs;
1059 }
Mark Brown010ff262009-08-17 17:39:22 +01001060 default:
1061 break;
1062 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001063
1064 if (widget->connected) {
1065 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001066 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1067 widget->inputs = snd_soc_dapm_suspend_check(widget);
1068 return widget->inputs;
1069 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001070
1071 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001072 if (widget->id == snd_soc_dapm_vmid) {
1073 widget->inputs = snd_soc_dapm_suspend_check(widget);
1074 return widget->inputs;
1075 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076
1077 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001078 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001079 (widget->id == snd_soc_dapm_line &&
1080 !list_empty(&widget->sinks))) {
1081 widget->inputs = snd_soc_dapm_suspend_check(widget);
1082 return widget->inputs;
1083 }
1084
Mark Brown1ab97c82011-11-27 16:21:51 +00001085 /* signal generator */
1086 if (widget->id == snd_soc_dapm_siggen) {
1087 widget->inputs = snd_soc_dapm_suspend_check(widget);
1088 return widget->inputs;
1089 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001090 }
1091
1092 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001093 DAPM_UPDATE_STAT(widget, neighbour_checks);
1094
Mark Brownbf3a9e12011-06-13 16:42:29 +01001095 if (path->weak)
1096 continue;
1097
Mark Brown8af294b2013-02-22 17:48:15 +00001098 if (path->walking)
1099 return 1;
1100
Richard Purdie2b97eab2006-10-06 18:32:18 +02001101 if (path->walked)
1102 continue;
1103
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001104 trace_snd_soc_dapm_input_path(widget, path);
1105
Richard Purdie2b97eab2006-10-06 18:32:18 +02001106 if (path->source && path->connect) {
1107 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001108 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001109
1110 /* do we need to add this widget to the list ? */
1111 if (list) {
1112 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001113 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001114 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001115 dev_err(widget->dapm->dev,
1116 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001117 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001118 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001119 return con;
1120 }
1121 }
1122
1123 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001124
1125 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126 }
1127 }
1128
Mark Brown024dc072011-10-09 11:52:05 +01001129 widget->inputs = con;
1130
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 return con;
1132}
1133
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001134/**
1135 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1136 * @dai: the soc DAI.
1137 * @stream: stream direction.
1138 * @list: list of active widgets for this stream.
1139 *
1140 * Queries DAPM graph as to whether an valid audio stream path exists for
1141 * the initial stream specified by name. This takes into account
1142 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1143 *
1144 * Returns the number of valid paths or negative error.
1145 */
1146int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1147 struct snd_soc_dapm_widget_list **list)
1148{
1149 struct snd_soc_card *card = dai->card;
1150 int paths;
1151
1152 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1153 dapm_reset(card);
1154
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001155 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001156 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001157 dapm_clear_walk_output(&card->dapm,
1158 &dai->playback_widget->sinks);
1159 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001160 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001161 dapm_clear_walk_input(&card->dapm,
1162 &dai->capture_widget->sources);
1163 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001164
1165 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001166 mutex_unlock(&card->dapm_mutex);
1167
1168 return paths;
1169}
1170
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001172 * Handler for generic register modifier widget.
1173 */
1174int dapm_reg_event(struct snd_soc_dapm_widget *w,
1175 struct snd_kcontrol *kcontrol, int event)
1176{
1177 unsigned int val;
1178
1179 if (SND_SOC_DAPM_EVENT_ON(event))
1180 val = w->on_val;
1181 else
1182 val = w->off_val;
1183
Liam Girdwood49575fb52012-03-06 18:16:19 +00001184 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001185 w->mask << w->shift, val << w->shift);
1186
1187 return 0;
1188}
Mark Brown11589412008-07-29 11:42:23 +01001189EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001190
Mark Brown62ea8742012-01-21 21:14:48 +00001191/*
1192 * Handler for regulator supply widget.
1193 */
1194int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1195 struct snd_kcontrol *kcontrol, int event)
1196{
Mark Brownc05b84d2012-09-07 12:57:11 +08001197 int ret;
1198
1199 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001200 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001201 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001202 if (ret != 0)
1203 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001204 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001205 w->name, ret);
1206 }
1207
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001208 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001209 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001210 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001211 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001212 if (ret != 0)
1213 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001214 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001215 w->name, ret);
1216 }
1217
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001218 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001219 }
Mark Brown62ea8742012-01-21 21:14:48 +00001220}
1221EXPORT_SYMBOL_GPL(dapm_regulator_event);
1222
Ola Liljad7e7eb92012-05-24 15:26:25 +02001223/*
1224 * Handler for clock supply widget.
1225 */
1226int dapm_clock_event(struct snd_soc_dapm_widget *w,
1227 struct snd_kcontrol *kcontrol, int event)
1228{
1229 if (!w->clk)
1230 return -EIO;
1231
Mark Brownec029952012-06-04 08:16:20 +01001232#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001233 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001234 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001235 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001236 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001237 return 0;
1238 }
Mark Brownec029952012-06-04 08:16:20 +01001239#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001240 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001241}
1242EXPORT_SYMBOL_GPL(dapm_clock_event);
1243
Mark Brownd8050022011-09-28 18:28:23 +01001244static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1245{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001246 if (w->power_checked)
1247 return w->new_power;
1248
Mark Brownd8050022011-09-28 18:28:23 +01001249 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001250 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001251 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001252 w->new_power = w->power_check(w);
1253
1254 w->power_checked = true;
1255
1256 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001257}
1258
Mark Browncd0f2d42009-04-20 16:56:59 +01001259/* Generic check to see if a widget should be powered.
1260 */
1261static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1262{
1263 int in, out;
1264
Mark Brownde02d072011-09-20 21:43:24 +01001265 DAPM_UPDATE_STAT(w, power_checks);
1266
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001267 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001268 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001269 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001270 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001271 return out != 0 && in != 0;
1272}
1273
Mark Brown6ea31b92009-04-20 17:15:41 +01001274/* Check to see if an ADC has power */
1275static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1276{
1277 int in;
1278
Mark Brownde02d072011-09-20 21:43:24 +01001279 DAPM_UPDATE_STAT(w, power_checks);
1280
Mark Brown6ea31b92009-04-20 17:15:41 +01001281 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001282 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001283 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001284 return in != 0;
1285 } else {
1286 return dapm_generic_check_power(w);
1287 }
1288}
1289
1290/* Check to see if a DAC has power */
1291static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1292{
1293 int out;
1294
Mark Brownde02d072011-09-20 21:43:24 +01001295 DAPM_UPDATE_STAT(w, power_checks);
1296
Mark Brown6ea31b92009-04-20 17:15:41 +01001297 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001298 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001299 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001300 return out != 0;
1301 } else {
1302 return dapm_generic_check_power(w);
1303 }
1304}
1305
Mark Brown246d0a12009-04-22 18:24:55 +01001306/* Check to see if a power supply is needed */
1307static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1308{
1309 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001310
Mark Brownde02d072011-09-20 21:43:24 +01001311 DAPM_UPDATE_STAT(w, power_checks);
1312
Mark Brown246d0a12009-04-22 18:24:55 +01001313 /* Check if one of our outputs is connected */
1314 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001315 DAPM_UPDATE_STAT(w, neighbour_checks);
1316
Mark Brownbf3a9e12011-06-13 16:42:29 +01001317 if (path->weak)
1318 continue;
1319
Mark Brown215edda2009-09-08 18:59:05 +01001320 if (path->connected &&
1321 !path->connected(path->source, path->sink))
1322 continue;
1323
Mark Brown30173582011-02-11 11:42:19 +00001324 if (!path->sink)
1325 continue;
1326
Mark Brownf68d7e12011-10-04 22:57:50 +01001327 if (dapm_widget_power_check(path->sink))
1328 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001329 }
1330
Mark Brownf68d7e12011-10-04 22:57:50 +01001331 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001332}
1333
Mark Brown35c64bca2011-09-28 18:23:53 +01001334static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1335{
1336 return 1;
1337}
1338
Mark Brown38357ab2009-06-06 19:03:23 +01001339static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1340 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001341 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001342{
Mark Brown828a8422011-01-15 13:14:30 +00001343 int *sort;
1344
1345 if (power_up)
1346 sort = dapm_up_seq;
1347 else
1348 sort = dapm_down_seq;
1349
Mark Brown38357ab2009-06-06 19:03:23 +01001350 if (sort[a->id] != sort[b->id])
1351 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001352 if (a->subseq != b->subseq) {
1353 if (power_up)
1354 return a->subseq - b->subseq;
1355 else
1356 return b->subseq - a->subseq;
1357 }
Mark Brownb22ead22009-06-07 12:51:26 +01001358 if (a->reg != b->reg)
1359 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001360 if (a->dapm != b->dapm)
1361 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001362
Mark Brown38357ab2009-06-06 19:03:23 +01001363 return 0;
1364}
Mark Brown42aa3412009-03-01 19:21:10 +00001365
Mark Brown38357ab2009-06-06 19:03:23 +01001366/* Insert a widget in order into a DAPM power sequence. */
1367static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1368 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001369 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001370{
1371 struct snd_soc_dapm_widget *w;
1372
1373 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001374 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001375 list_add_tail(&new_widget->power_list, &w->power_list);
1376 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001377 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001378
Mark Brown38357ab2009-06-06 19:03:23 +01001379 list_add_tail(&new_widget->power_list, list);
1380}
Mark Brown42aa3412009-03-01 19:21:10 +00001381
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001382static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001383 struct snd_soc_dapm_widget *w, int event)
1384{
Mark Brown68f89ad2010-11-03 23:51:49 -04001385 const char *ev_name;
1386 int power, ret;
1387
1388 switch (event) {
1389 case SND_SOC_DAPM_PRE_PMU:
1390 ev_name = "PRE_PMU";
1391 power = 1;
1392 break;
1393 case SND_SOC_DAPM_POST_PMU:
1394 ev_name = "POST_PMU";
1395 power = 1;
1396 break;
1397 case SND_SOC_DAPM_PRE_PMD:
1398 ev_name = "PRE_PMD";
1399 power = 0;
1400 break;
1401 case SND_SOC_DAPM_POST_PMD:
1402 ev_name = "POST_PMD";
1403 power = 0;
1404 break;
Mark Brown80114122013-02-25 15:14:19 +00001405 case SND_SOC_DAPM_WILL_PMU:
1406 ev_name = "WILL_PMU";
1407 power = 1;
1408 break;
1409 case SND_SOC_DAPM_WILL_PMD:
1410 ev_name = "WILL_PMD";
1411 power = 0;
1412 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001413 default:
1414 BUG();
1415 return;
1416 }
1417
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001418 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001419 return;
1420
1421 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001422 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001423 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001424 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001425 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001426 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001427 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001428 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001429 ev_name, w->name, ret);
1430 }
1431}
1432
Mark Brownb22ead22009-06-07 12:51:26 +01001433/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001434static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001435 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001436{
Mark Brown68f89ad2010-11-03 23:51:49 -04001437 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001438 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001439 unsigned int value = 0;
1440 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001441
1442 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1443 power_list)->reg;
1444
1445 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001446 BUG_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001447 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001448
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001449 mask |= w->mask << w->shift;
1450 if (w->power)
1451 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001452 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001453 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001454
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001455 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001456 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1457 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001458
Mark Brown68f89ad2010-11-03 23:51:49 -04001459 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001460 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1461 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001462 }
1463
Mark Brown81628102009-06-07 13:21:24 +01001464 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001465 /* Any widget will do, they should all be updating the
1466 * same register.
1467 */
1468 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1469 power_list);
1470
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001471 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001472 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001473 value, mask, reg, card->pop_time);
1474 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001475 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001476 }
1477
1478 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001479 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1480 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001481 }
Mark Brown42aa3412009-03-01 19:21:10 +00001482}
1483
Mark Brownb22ead22009-06-07 12:51:26 +01001484/* Apply a DAPM power sequence.
1485 *
1486 * We walk over a pre-sorted list of widgets to apply power to. In
1487 * order to minimise the number of writes to the device required
1488 * multiple widgets will be updated in a single write where possible.
1489 * Currently anything that requires more than a single write is not
1490 * handled.
1491 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001492static void dapm_seq_run(struct snd_soc_card *card,
1493 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001494{
1495 struct snd_soc_dapm_widget *w, *n;
1496 LIST_HEAD(pending);
1497 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001498 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001499 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001500 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001501 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001502 int *sort;
1503
1504 if (power_up)
1505 sort = dapm_up_seq;
1506 else
1507 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001508
Mark Brownb22ead22009-06-07 12:51:26 +01001509 list_for_each_entry_safe(w, n, list, power_list) {
1510 ret = 0;
1511
1512 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001513 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001514 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001515 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001516 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001517
Mark Brown474b62d2011-01-18 16:14:44 +00001518 if (cur_dapm && cur_dapm->seq_notifier) {
1519 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1520 if (sort[i] == cur_sort)
1521 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001522 i,
1523 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001524 }
1525
Mark Brownb22ead22009-06-07 12:51:26 +01001526 INIT_LIST_HEAD(&pending);
1527 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001528 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001529 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001530 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001531 }
1532
Mark Brown163cac02009-06-07 10:12:52 +01001533 switch (w->id) {
1534 case snd_soc_dapm_pre:
1535 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001536 list_for_each_entry_safe_continue(w, n, list,
1537 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001538
Mark Brownb22ead22009-06-07 12:51:26 +01001539 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001540 ret = w->event(w,
1541 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001542 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001543 ret = w->event(w,
1544 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001545 break;
1546
1547 case snd_soc_dapm_post:
1548 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001549 list_for_each_entry_safe_continue(w, n, list,
1550 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001551
Mark Brownb22ead22009-06-07 12:51:26 +01001552 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001553 ret = w->event(w,
1554 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001555 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001556 ret = w->event(w,
1557 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001558 break;
1559
Mark Brown163cac02009-06-07 10:12:52 +01001560 default:
Mark Brown81628102009-06-07 13:21:24 +01001561 /* Queue it up for application */
1562 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001563 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001564 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001565 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001566 list_move(&w->power_list, &pending);
1567 break;
Mark Brown163cac02009-06-07 10:12:52 +01001568 }
Mark Brownb22ead22009-06-07 12:51:26 +01001569
1570 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001571 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001572 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001573 }
Mark Brownb22ead22009-06-07 12:51:26 +01001574
1575 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001576 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001577
1578 if (cur_dapm && cur_dapm->seq_notifier) {
1579 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1580 if (sort[i] == cur_sort)
1581 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001582 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001583 }
Mark Brown163cac02009-06-07 10:12:52 +01001584}
1585
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001586static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001587{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001588 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001589 struct snd_soc_dapm_widget_list *wlist;
1590 struct snd_soc_dapm_widget *w = NULL;
1591 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001592 int ret;
1593
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001594 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001595 return;
1596
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001597 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001598
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001599 for (wi = 0; wi < wlist->num_widgets; wi++) {
1600 w = wlist->widgets[wi];
1601
1602 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1603 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1604 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001605 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001606 w->name, ret);
1607 }
Mark Brown97404f22010-12-14 16:13:57 +00001608 }
1609
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001610 if (!w)
1611 return;
1612
Liam Girdwood49575fb52012-03-06 18:16:19 +00001613 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001614 update->val);
1615 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001616 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001617 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001618
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001619 for (wi = 0; wi < wlist->num_widgets; wi++) {
1620 w = wlist->widgets[wi];
1621
1622 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1623 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1624 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001625 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001626 w->name, ret);
1627 }
Mark Brown97404f22010-12-14 16:13:57 +00001628 }
1629}
1630
Mark Brown9d0624a2011-02-18 11:49:43 -08001631/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1632 * they're changing state.
1633 */
1634static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1635{
1636 struct snd_soc_dapm_context *d = data;
1637 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001638
Mark Brown56fba412011-06-04 11:25:10 +01001639 /* If we're off and we're not supposed to be go into STANDBY */
1640 if (d->bias_level == SND_SOC_BIAS_OFF &&
1641 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001642 if (d->dev)
1643 pm_runtime_get_sync(d->dev);
1644
Mark Brown9d0624a2011-02-18 11:49:43 -08001645 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1646 if (ret != 0)
1647 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001648 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001649 }
1650
Mark Brown56fba412011-06-04 11:25:10 +01001651 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1652 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001653 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1654 if (ret != 0)
1655 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001656 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001657 }
1658}
1659
1660/* Async callback run prior to DAPM sequences - brings to their final
1661 * state.
1662 */
1663static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1664{
1665 struct snd_soc_dapm_context *d = data;
1666 int ret;
1667
1668 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001669 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1670 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1671 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001672 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1673 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001674 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001675 ret);
1676 }
1677
1678 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001679 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1680 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001681 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1682 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001683 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1684 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001685
1686 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001687 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001688 }
1689
1690 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001691 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1692 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001693 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1694 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001695 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001696 ret);
1697 }
1698}
Mark Brown97404f22010-12-14 16:13:57 +00001699
Mark Brownfe4fda52011-10-03 22:36:57 +01001700static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1701 bool power, bool connect)
1702{
1703 /* If a connection is being made or broken then that update
1704 * will have marked the peer dirty, otherwise the widgets are
1705 * not connected and this update has no impact. */
1706 if (!connect)
1707 return;
1708
1709 /* If the peer is already in the state we're moving to then we
1710 * won't have an impact on it. */
1711 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001712 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001713}
1714
Mark Brown05623c42011-09-28 17:02:31 +01001715static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1716 struct list_head *up_list,
1717 struct list_head *down_list)
1718{
Mark Browndb432b42011-10-03 21:06:40 +01001719 struct snd_soc_dapm_path *path;
1720
Mark Brown05623c42011-09-28 17:02:31 +01001721 if (w->power == power)
1722 return;
1723
1724 trace_snd_soc_dapm_widget_power(w, power);
1725
Mark Browndb432b42011-10-03 21:06:40 +01001726 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001727 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001728 */
1729 list_for_each_entry(path, &w->sources, list_sink) {
1730 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001731 dapm_widget_set_peer_power(path->source, power,
1732 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001733 }
1734 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001735 switch (w->id) {
1736 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001737 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001738 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001739 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001740 /* Supplies can't affect their outputs, only their inputs */
1741 break;
1742 default:
1743 list_for_each_entry(path, &w->sinks, list_source) {
1744 if (path->sink) {
1745 dapm_widget_set_peer_power(path->sink, power,
1746 path->connect);
1747 }
Mark Browndb432b42011-10-03 21:06:40 +01001748 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001749 break;
Mark Browndb432b42011-10-03 21:06:40 +01001750 }
1751
Mark Brown05623c42011-09-28 17:02:31 +01001752 if (power)
1753 dapm_seq_insert(w, up_list, true);
1754 else
1755 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001756}
1757
Mark Brown7c81beb2011-09-20 22:22:32 +01001758static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1759 struct list_head *up_list,
1760 struct list_head *down_list)
1761{
Mark Brown7c81beb2011-09-20 22:22:32 +01001762 int power;
1763
1764 switch (w->id) {
1765 case snd_soc_dapm_pre:
1766 dapm_seq_insert(w, down_list, false);
1767 break;
1768 case snd_soc_dapm_post:
1769 dapm_seq_insert(w, up_list, true);
1770 break;
1771
1772 default:
Mark Brownd8050022011-09-28 18:28:23 +01001773 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001774
Mark Brown05623c42011-09-28 17:02:31 +01001775 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001776 break;
1777 }
1778}
1779
Mark Brown42aa3412009-03-01 19:21:10 +00001780/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 * Scan each dapm widget for complete audio path.
1782 * A complete path is a route that has valid endpoints i.e.:-
1783 *
1784 * o DAC to output pin.
1785 * o Input Pin to ADC.
1786 * o Input pin to Output pin (bypass, sidetone)
1787 * o DAC to ADC (loopback).
1788 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001789static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790{
1791 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001792 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001793 LIST_HEAD(up_list);
1794 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001795 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001796 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001797
Mark Brown84e90932010-11-04 00:07:02 -04001798 trace_snd_soc_dapm_start(card);
1799
Mark Brown56fba412011-06-04 11:25:10 +01001800 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098b2012-03-08 15:06:09 +00001801 if (d->idle_bias_off)
1802 d->target_bias_level = SND_SOC_BIAS_OFF;
1803 else
1804 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001805 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001806
Liam Girdwood6c120e12012-02-15 15:15:34 +00001807 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001808
Mark Brown6d3ddc82009-05-16 17:47:29 +01001809 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001810 * lists indicating if they should be powered up or down. We
1811 * only check widgets that have been flagged as dirty but note
1812 * that new widgets may be added to the dirty list while we
1813 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001814 */
Mark Browndb432b42011-10-03 21:06:40 +01001815 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001816 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001817 }
1818
Mark Brownf9de6d72011-09-28 17:19:47 +01001819 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001820 switch (w->id) {
1821 case snd_soc_dapm_pre:
1822 case snd_soc_dapm_post:
1823 /* These widgets always need to be powered */
1824 break;
1825 default:
1826 list_del_init(&w->dirty);
1827 break;
1828 }
Mark Browndb432b42011-10-03 21:06:40 +01001829
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001830 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001831 d = w->dapm;
1832
1833 /* Supplies and micbiases only bring the
1834 * context up to STANDBY as unless something
1835 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001836 * generally don't require full power. Signal
1837 * generators are virtual pins and have no
1838 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001839 */
1840 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001841 case snd_soc_dapm_siggen:
1842 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001843 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001844 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001845 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001846 case snd_soc_dapm_micbias:
1847 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1848 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1849 break;
1850 default:
1851 d->target_bias_level = SND_SOC_BIAS_ON;
1852 break;
1853 }
1854 }
1855
1856 }
1857
Mark Brown85a843c2011-09-21 21:29:47 +01001858 /* Force all contexts in the card to the same bias state if
1859 * they're not ground referenced.
1860 */
Mark Brown56fba412011-06-04 11:25:10 +01001861 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001862 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001863 if (d->target_bias_level > bias)
1864 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001865 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001866 if (!d->idle_bias_off)
1867 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001868
Mark Brownde02d072011-09-20 21:43:24 +01001869 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001870
Mark Brown9d0624a2011-02-18 11:49:43 -08001871 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001872 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001873 async_schedule_domain(dapm_pre_sequence_async, d,
1874 &async_domain);
1875 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001876
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001877 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001878 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001879 }
1880
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001881 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001882 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001883 }
1884
Mark Brown6d3ddc82009-05-16 17:47:29 +01001885 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001886 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001887
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001888 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001889
Mark Brown6d3ddc82009-05-16 17:47:29 +01001890 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001891 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001892
Mark Brown9d0624a2011-02-18 11:49:43 -08001893 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001894 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001895 async_schedule_domain(dapm_post_sequence_async, d,
1896 &async_domain);
1897 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001898
Liam Girdwood8078d872012-02-15 15:15:35 +00001899 /* do we need to notify any clients that DAPM event is complete */
1900 list_for_each_entry(d, &card->dapm_list, list) {
1901 if (d->stream_event)
1902 d->stream_event(d, event);
1903 }
1904
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001905 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001906 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001907 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001908
Mark Brown84e90932010-11-04 00:07:02 -04001909 trace_snd_soc_dapm_done(card);
1910
Mark Brown42aa3412009-03-01 19:21:10 +00001911 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912}
1913
Mark Brown79fb9382009-08-21 16:38:13 +01001914#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001915static ssize_t dapm_widget_power_read_file(struct file *file,
1916 char __user *user_buf,
1917 size_t count, loff_t *ppos)
1918{
1919 struct snd_soc_dapm_widget *w = file->private_data;
1920 char *buf;
1921 int in, out;
1922 ssize_t ret;
1923 struct snd_soc_dapm_path *p = NULL;
1924
1925 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1926 if (!buf)
1927 return -ENOMEM;
1928
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001929 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001930 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001931 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001932 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001933
Mark Brownf13ebad2012-03-03 18:01:01 +00001934 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1935 w->name, w->power ? "On" : "Off",
1936 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001937
Mark Brownd033c362009-12-04 15:25:56 +00001938 if (w->reg >= 0)
1939 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001940 " - R%d(0x%x) mask 0x%x",
1941 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001942
1943 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1944
Mark Brown3eef08b2009-09-14 16:49:00 +01001945 if (w->sname)
1946 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1947 w->sname,
1948 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001949
1950 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001951 if (p->connected && !p->connected(w, p->sink))
1952 continue;
1953
Mark Brown79fb9382009-08-21 16:38:13 +01001954 if (p->connect)
1955 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001956 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001957 p->name ? p->name : "static",
1958 p->source->name);
1959 }
1960 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001961 if (p->connected && !p->connected(w, p->sink))
1962 continue;
1963
Mark Brown79fb9382009-08-21 16:38:13 +01001964 if (p->connect)
1965 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001966 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001967 p->name ? p->name : "static",
1968 p->sink->name);
1969 }
1970
1971 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1972
1973 kfree(buf);
1974 return ret;
1975}
1976
1977static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001978 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001979 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001980 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001981};
1982
Mark Brownef49e4f2011-04-04 20:48:13 +09001983static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1984 size_t count, loff_t *ppos)
1985{
1986 struct snd_soc_dapm_context *dapm = file->private_data;
1987 char *level;
1988
1989 switch (dapm->bias_level) {
1990 case SND_SOC_BIAS_ON:
1991 level = "On\n";
1992 break;
1993 case SND_SOC_BIAS_PREPARE:
1994 level = "Prepare\n";
1995 break;
1996 case SND_SOC_BIAS_STANDBY:
1997 level = "Standby\n";
1998 break;
1999 case SND_SOC_BIAS_OFF:
2000 level = "Off\n";
2001 break;
2002 default:
2003 BUG();
2004 level = "Unknown\n";
2005 break;
2006 }
2007
2008 return simple_read_from_buffer(user_buf, count, ppos, level,
2009 strlen(level));
2010}
2011
2012static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002013 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002014 .read = dapm_bias_read_file,
2015 .llseek = default_llseek,
2016};
2017
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002018void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2019 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002020{
Mark Brown79fb9382009-08-21 16:38:13 +01002021 struct dentry *d;
2022
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002023 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2024
2025 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002026 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002027 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002028 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002029 }
Mark Brown79fb9382009-08-21 16:38:13 +01002030
Mark Brownef49e4f2011-04-04 20:48:13 +09002031 d = debugfs_create_file("bias_level", 0444,
2032 dapm->debugfs_dapm, dapm,
2033 &dapm_bias_fops);
2034 if (!d)
2035 dev_warn(dapm->dev,
2036 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002037}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002038
2039static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2040{
2041 struct snd_soc_dapm_context *dapm = w->dapm;
2042 struct dentry *d;
2043
2044 if (!dapm->debugfs_dapm || !w->name)
2045 return;
2046
2047 d = debugfs_create_file(w->name, 0444,
2048 dapm->debugfs_dapm, w,
2049 &dapm_widget_power_fops);
2050 if (!d)
2051 dev_warn(w->dapm->dev,
2052 "ASoC: Failed to create %s debugfs file\n",
2053 w->name);
2054}
2055
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002056static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2057{
2058 debugfs_remove_recursive(dapm->debugfs_dapm);
2059}
2060
Mark Brown79fb9382009-08-21 16:38:13 +01002061#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002062void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2063 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002064{
2065}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002066
2067static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2068{
2069}
2070
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002071static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2072{
2073}
2074
Mark Brown79fb9382009-08-21 16:38:13 +01002075#endif
2076
Richard Purdie2b97eab2006-10-06 18:32:18 +02002077/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002078static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002079 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080{
2081 struct snd_soc_dapm_path *path;
2082 int found = 0;
2083
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002085 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002086 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 continue;
2088
2089 found = 1;
2090 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002091 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002093 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002094 } else {
2095 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002096 dapm_mark_dirty(path->source,
2097 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002099 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002100 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 }
2102
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002103 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002104 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002105
Liam Girdwood618dae12012-04-25 12:12:51 +01002106 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002108
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002109int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002110 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2111 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002112{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002113 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002114 int ret;
2115
Liam Girdwood3cd04342012-03-09 12:02:08 +00002116 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002117 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002118 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002119 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002120 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002121 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002122 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002123 return ret;
2124}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002125EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126
Milan plzik1b075e32008-01-10 14:39:46 +01002127/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002128static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002129 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130{
2131 struct snd_soc_dapm_path *path;
2132 int found = 0;
2133
Richard Purdie2b97eab2006-10-06 18:32:18 +02002134 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002135 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002137 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002138 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002139 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140 }
2141
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002142 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002143 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144
Liam Girdwood618dae12012-04-25 12:12:51 +01002145 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002147
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002148int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002149 struct snd_kcontrol *kcontrol, int connect,
2150 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002151{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002152 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002153 int ret;
2154
Liam Girdwood3cd04342012-03-09 12:02:08 +00002155 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002156 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002157 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002158 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002159 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002160 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002161 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002162 return ret;
2163}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002164EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165
2166/* show dapm widget status in sys fs */
2167static ssize_t dapm_widget_show(struct device *dev,
2168 struct device_attribute *attr, char *buf)
2169{
Mark Brown36ae1a92012-01-06 17:12:45 -08002170 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002171 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172 struct snd_soc_dapm_widget *w;
2173 int count = 0;
2174 char *state = "not set";
2175
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002176 list_for_each_entry(w, &codec->card->widgets, list) {
2177 if (w->dapm != &codec->dapm)
2178 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179
2180 /* only display widgets that burnm power */
2181 switch (w->id) {
2182 case snd_soc_dapm_hp:
2183 case snd_soc_dapm_mic:
2184 case snd_soc_dapm_spk:
2185 case snd_soc_dapm_line:
2186 case snd_soc_dapm_micbias:
2187 case snd_soc_dapm_dac:
2188 case snd_soc_dapm_adc:
2189 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002190 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002192 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002193 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002194 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002195 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 if (w->name)
2197 count += sprintf(buf + count, "%s: %s\n",
2198 w->name, w->power ? "On":"Off");
2199 break;
2200 default:
2201 break;
2202 }
2203 }
2204
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002205 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002206 case SND_SOC_BIAS_ON:
2207 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208 break;
Mark Brown0be98982008-05-19 12:31:28 +02002209 case SND_SOC_BIAS_PREPARE:
2210 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 break;
Mark Brown0be98982008-05-19 12:31:28 +02002212 case SND_SOC_BIAS_STANDBY:
2213 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 break;
Mark Brown0be98982008-05-19 12:31:28 +02002215 case SND_SOC_BIAS_OFF:
2216 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 break;
2218 }
2219 count += sprintf(buf + count, "PM State: %s\n", state);
2220
2221 return count;
2222}
2223
2224static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2225
2226int snd_soc_dapm_sys_add(struct device *dev)
2227{
Troy Kisky12ef1932008-10-13 17:42:14 -07002228 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229}
2230
2231static void snd_soc_dapm_sys_remove(struct device *dev)
2232{
Mark Brownaef90842009-05-16 17:53:16 +01002233 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234}
2235
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002236static void dapm_free_path(struct snd_soc_dapm_path *path)
2237{
2238 list_del(&path->list_sink);
2239 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002240 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002241 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002242 kfree(path);
2243}
2244
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002246static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247{
2248 struct snd_soc_dapm_widget *w, *next_w;
2249 struct snd_soc_dapm_path *p, *next_p;
2250
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002251 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2252 if (w->dapm != dapm)
2253 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002255 /*
2256 * remove source and sink paths associated to this widget.
2257 * While removing the path, remove reference to it from both
2258 * source and sink widgets so that path is removed only once.
2259 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002260 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2261 dapm_free_path(p);
2262
2263 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2264 dapm_free_path(p);
2265
Stephen Warrenfad59882011-04-28 17:37:59 -06002266 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002267 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002268 kfree(w);
2269 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270}
2271
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002272static struct snd_soc_dapm_widget *dapm_find_widget(
2273 struct snd_soc_dapm_context *dapm, const char *pin,
2274 bool search_other_contexts)
2275{
2276 struct snd_soc_dapm_widget *w;
2277 struct snd_soc_dapm_widget *fallback = NULL;
2278
2279 list_for_each_entry(w, &dapm->card->widgets, list) {
2280 if (!strcmp(w->name, pin)) {
2281 if (w->dapm == dapm)
2282 return w;
2283 else
2284 fallback = w;
2285 }
2286 }
2287
2288 if (search_other_contexts)
2289 return fallback;
2290
2291 return NULL;
2292}
2293
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002294static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002295 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002296{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002297 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002298
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002299 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002300 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002301 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002302 }
2303
Mark Brown1a8b2d92012-02-16 11:50:07 -08002304 if (w->connected != status)
2305 dapm_mark_dirty(w, "pin configuration");
2306
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002307 w->connected = status;
2308 if (status == 0)
2309 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002310
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002311 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002312}
2313
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002315 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002316 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 *
2318 * Walks all dapm audio paths and powers widgets according to their
2319 * stream or path usage.
2320 *
2321 * Returns 0 for success.
2322 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002323int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002325 int ret;
2326
Mark Brown4f4c0072011-10-07 14:29:19 +01002327 /*
2328 * Suppress early reports (eg, jacks syncing their state) to avoid
2329 * silly DAPM runs during card startup.
2330 */
2331 if (!dapm->card || !dapm->card->instantiated)
2332 return 0;
2333
Liam Girdwood3cd04342012-03-09 12:02:08 +00002334 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002335 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002336 mutex_unlock(&dapm->card->dapm_mutex);
2337 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338}
Liam Girdwooda5302182008-07-07 13:35:17 +01002339EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002341static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2342 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2343 const char *control,
2344 int (*connected)(struct snd_soc_dapm_widget *source,
2345 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346{
2347 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002348 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349
2350 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2351 if (!path)
2352 return -ENOMEM;
2353
2354 path->source = wsource;
2355 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002356 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 INIT_LIST_HEAD(&path->list);
2358 INIT_LIST_HEAD(&path->list_source);
2359 INIT_LIST_HEAD(&path->list_sink);
2360
2361 /* check for external widgets */
2362 if (wsink->id == snd_soc_dapm_input) {
2363 if (wsource->id == snd_soc_dapm_micbias ||
2364 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002365 wsource->id == snd_soc_dapm_line ||
2366 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 wsink->ext = 1;
2368 }
2369 if (wsource->id == snd_soc_dapm_output) {
2370 if (wsink->id == snd_soc_dapm_spk ||
2371 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002372 wsink->id == snd_soc_dapm_line ||
2373 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 wsource->ext = 1;
2375 }
2376
2377 /* connect static paths */
2378 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002379 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380 list_add(&path->list_sink, &wsink->sources);
2381 list_add(&path->list_source, &wsource->sinks);
2382 path->connect = 1;
2383 return 0;
2384 }
2385
2386 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002387 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 case snd_soc_dapm_adc:
2389 case snd_soc_dapm_dac:
2390 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002391 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002392 case snd_soc_dapm_input:
2393 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002394 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395 case snd_soc_dapm_micbias:
2396 case snd_soc_dapm_vmid:
2397 case snd_soc_dapm_pre:
2398 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002399 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002400 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002401 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002402 case snd_soc_dapm_aif_in:
2403 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002404 case snd_soc_dapm_dai_in:
2405 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002406 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002407 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002408 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409 list_add(&path->list_sink, &wsink->sources);
2410 list_add(&path->list_source, &wsource->sinks);
2411 path->connect = 1;
2412 return 0;
2413 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002414 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002415 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002416 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002417 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 if (ret != 0)
2419 goto err;
2420 break;
2421 case snd_soc_dapm_switch:
2422 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002423 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002424 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 if (ret != 0)
2426 goto err;
2427 break;
2428 case snd_soc_dapm_hp:
2429 case snd_soc_dapm_mic:
2430 case snd_soc_dapm_line:
2431 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002432 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 list_add(&path->list_sink, &wsink->sources);
2434 list_add(&path->list_source, &wsource->sinks);
2435 path->connect = 0;
2436 return 0;
2437 }
Mark Brownfabd0382012-07-05 17:20:06 +01002438
2439 dapm_mark_dirty(wsource, "Route added");
2440 dapm_mark_dirty(wsink, "Route added");
2441
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002443err:
2444 kfree(path);
2445 return ret;
2446}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002448static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2449 const struct snd_soc_dapm_route *route)
2450{
2451 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2452 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2453 const char *sink;
2454 const char *source;
2455 char prefixed_sink[80];
2456 char prefixed_source[80];
2457 int ret;
2458
2459 if (dapm->codec && dapm->codec->name_prefix) {
2460 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2461 dapm->codec->name_prefix, route->sink);
2462 sink = prefixed_sink;
2463 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2464 dapm->codec->name_prefix, route->source);
2465 source = prefixed_source;
2466 } else {
2467 sink = route->sink;
2468 source = route->source;
2469 }
2470
2471 /*
2472 * find src and dest widgets over all widgets but favor a widget from
2473 * current DAPM context
2474 */
2475 list_for_each_entry(w, &dapm->card->widgets, list) {
2476 if (!wsink && !(strcmp(w->name, sink))) {
2477 wtsink = w;
2478 if (w->dapm == dapm)
2479 wsink = w;
2480 continue;
2481 }
2482 if (!wsource && !(strcmp(w->name, source))) {
2483 wtsource = w;
2484 if (w->dapm == dapm)
2485 wsource = w;
2486 }
2487 }
2488 /* use widget from another DAPM context if not found from this */
2489 if (!wsink)
2490 wsink = wtsink;
2491 if (!wsource)
2492 wsource = wtsource;
2493
2494 if (wsource == NULL) {
2495 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2496 route->source);
2497 return -ENODEV;
2498 }
2499 if (wsink == NULL) {
2500 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2501 route->sink);
2502 return -ENODEV;
2503 }
2504
2505 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2506 route->connected);
2507 if (ret)
2508 goto err;
2509
2510 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002512 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002513 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002514 return ret;
2515}
Mark Brown105f1c22008-05-13 14:52:19 +02002516
Mark Brownefcc3c62012-07-05 17:24:19 +01002517static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2518 const struct snd_soc_dapm_route *route)
2519{
2520 struct snd_soc_dapm_path *path, *p;
2521 const char *sink;
2522 const char *source;
2523 char prefixed_sink[80];
2524 char prefixed_source[80];
2525
2526 if (route->control) {
2527 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002528 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002529 return -EINVAL;
2530 }
2531
2532 if (dapm->codec && dapm->codec->name_prefix) {
2533 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2534 dapm->codec->name_prefix, route->sink);
2535 sink = prefixed_sink;
2536 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2537 dapm->codec->name_prefix, route->source);
2538 source = prefixed_source;
2539 } else {
2540 sink = route->sink;
2541 source = route->source;
2542 }
2543
2544 path = NULL;
2545 list_for_each_entry(p, &dapm->card->paths, list) {
2546 if (strcmp(p->source->name, source) != 0)
2547 continue;
2548 if (strcmp(p->sink->name, sink) != 0)
2549 continue;
2550 path = p;
2551 break;
2552 }
2553
2554 if (path) {
2555 dapm_mark_dirty(path->source, "Route removed");
2556 dapm_mark_dirty(path->sink, "Route removed");
2557
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002558 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002559 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002560 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002561 source, sink);
2562 }
2563
2564 return 0;
2565}
2566
Mark Brown105f1c22008-05-13 14:52:19 +02002567/**
Mark Brown105f1c22008-05-13 14:52:19 +02002568 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002569 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002570 * @route: audio routes
2571 * @num: number of routes
2572 *
2573 * Connects 2 dapm widgets together via a named audio path. The sink is
2574 * the widget receiving the audio signal, whilst the source is the sender
2575 * of the audio signal.
2576 *
2577 * Returns 0 for success else error. On error all resources can be freed
2578 * with a call to snd_soc_card_free().
2579 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002580int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002581 const struct snd_soc_dapm_route *route, int num)
2582{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002583 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002584
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002585 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002586 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002587 r = snd_soc_dapm_add_route(dapm, route);
2588 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002589 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2590 route->source,
2591 route->control ? route->control : "direct",
2592 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002593 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002594 }
2595 route++;
2596 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002597 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002598
Dan Carpenter60884c22012-04-13 22:25:43 +03002599 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002600}
2601EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2602
Mark Brownefcc3c62012-07-05 17:24:19 +01002603/**
2604 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2605 * @dapm: DAPM context
2606 * @route: audio routes
2607 * @num: number of routes
2608 *
2609 * Removes routes from the DAPM context.
2610 */
2611int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2612 const struct snd_soc_dapm_route *route, int num)
2613{
2614 int i, ret = 0;
2615
2616 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2617 for (i = 0; i < num; i++) {
2618 snd_soc_dapm_del_route(dapm, route);
2619 route++;
2620 }
2621 mutex_unlock(&dapm->card->dapm_mutex);
2622
2623 return ret;
2624}
2625EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2626
Mark Brownbf3a9e12011-06-13 16:42:29 +01002627static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2628 const struct snd_soc_dapm_route *route)
2629{
2630 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2631 route->source,
2632 true);
2633 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2634 route->sink,
2635 true);
2636 struct snd_soc_dapm_path *path;
2637 int count = 0;
2638
2639 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002640 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002641 route->source);
2642 return -ENODEV;
2643 }
2644
2645 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002646 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002647 route->sink);
2648 return -ENODEV;
2649 }
2650
2651 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002652 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002653 route->source, route->sink);
2654
2655 list_for_each_entry(path, &source->sinks, list_source) {
2656 if (path->sink == sink) {
2657 path->weak = 1;
2658 count++;
2659 }
2660 }
2661
2662 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002663 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002664 route->source, route->sink);
2665 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002666 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002667 count, route->source, route->sink);
2668
2669 return 0;
2670}
2671
2672/**
2673 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2674 * @dapm: DAPM context
2675 * @route: audio routes
2676 * @num: number of routes
2677 *
2678 * Mark existing routes matching those specified in the passed array
2679 * as being weak, meaning that they are ignored for the purpose of
2680 * power decisions. The main intended use case is for sidetone paths
2681 * which couple audio between other independent paths if they are both
2682 * active in order to make the combination work better at the user
2683 * level but which aren't intended to be "used".
2684 *
2685 * Note that CODEC drivers should not use this as sidetone type paths
2686 * can frequently also be used as bypass paths.
2687 */
2688int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2689 const struct snd_soc_dapm_route *route, int num)
2690{
2691 int i, err;
2692 int ret = 0;
2693
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002694 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002695 for (i = 0; i < num; i++) {
2696 err = snd_soc_dapm_weak_route(dapm, route);
2697 if (err)
2698 ret = err;
2699 route++;
2700 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002701 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002702
2703 return ret;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2706
Mark Brown105f1c22008-05-13 14:52:19 +02002707/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002708 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002709 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710 *
2711 * Checks the codec for any new dapm widgets and creates them if found.
2712 *
2713 * Returns 0 for success.
2714 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002715int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002717 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002719 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002721 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002722
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002723 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724 {
2725 if (w->new)
2726 continue;
2727
Stephen Warrenfad59882011-04-28 17:37:59 -06002728 if (w->num_kcontrols) {
2729 w->kcontrols = kzalloc(w->num_kcontrols *
2730 sizeof(struct snd_kcontrol *),
2731 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002732 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002733 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002734 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002735 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002736 }
2737
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 switch(w->id) {
2739 case snd_soc_dapm_switch:
2740 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002741 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002742 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002743 break;
2744 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002745 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002746 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002747 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002750 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002751 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002752 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002753 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 break;
2755 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002756
2757 /* Read the initial power state from the device */
2758 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002759 val = soc_widget_read(w, w->reg) >> w->shift;
2760 val &= w->mask;
2761 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002762 w->power = 1;
2763 }
2764
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002766
Mark Brown7508b122011-10-05 12:09:12 +01002767 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002768 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 }
2770
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002771 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2772 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773 return 0;
2774}
2775EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2776
2777/**
2778 * snd_soc_dapm_get_volsw - dapm mixer get callback
2779 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002780 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 *
2782 * Callback to get the value of a dapm mixer control.
2783 *
2784 * Returns 0 for success.
2785 */
2786int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2788{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002789 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002790 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002791 struct soc_mixer_control *mc =
2792 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002793 unsigned int reg = mc->reg;
2794 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002795 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002796 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002797 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002798 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002799
2800 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002801 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002802 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002803 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002805 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2806 if (dapm_kcontrol_is_powered(kcontrol))
2807 val = (snd_soc_read(codec, reg) >> shift) & mask;
2808 else
2809 val = dapm_kcontrol_get_value(kcontrol);
2810 mutex_unlock(&card->dapm_mutex);
2811
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002812 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002813 ucontrol->value.integer.value[0] = max - val;
2814 else
2815 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816
2817 return 0;
2818}
2819EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2820
2821/**
2822 * snd_soc_dapm_put_volsw - dapm mixer set callback
2823 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002824 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825 *
2826 * Callback to set the value of a dapm mixer control.
2827 *
2828 * Returns 0 for success.
2829 */
2830int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2832{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002833 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002834 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002835 struct soc_mixer_control *mc =
2836 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002837 unsigned int reg = mc->reg;
2838 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002839 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002840 unsigned int mask = (1 << fls(max)) - 1;
2841 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002842 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002843 int connect, change;
2844 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002846 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002847 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002848 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002849 kcontrol->id.name);
2850
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002852 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002853
2854 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002855 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856
Liam Girdwood3cd04342012-03-09 12:02:08 +00002857 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002858
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002859 dapm_kcontrol_set_value(kcontrol, val);
2860
2861 mask = mask << shift;
2862 val = val << shift;
2863
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002864 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002865 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002866 update.kcontrol = kcontrol;
2867 update.reg = reg;
2868 update.mask = mask;
2869 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002870
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002871 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002872
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002873 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002874
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002875 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002876 }
2877
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002878 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002879 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002880}
2881EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2882
2883/**
2884 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2885 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002886 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887 *
2888 * Callback to get the value of a dapm enumerated double mixer control.
2889 *
2890 * Returns 0 for success.
2891 */
2892int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2894{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002895 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002896 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002897 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002898
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002899 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002900 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002901 if (e->shift_l != e->shift_r)
2902 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002903 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002904
2905 return 0;
2906}
2907EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2908
2909/**
2910 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2911 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002912 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002913 *
2914 * Callback to set the value of a dapm enumerated double mixer control.
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002921 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002922 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002923 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002924 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002925 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002926 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002927
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002928 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929 return -EINVAL;
2930 mux = ucontrol->value.enumerated.item[0];
2931 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002932 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002934 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935 return -EINVAL;
2936 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002937 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002938 }
2939
Liam Girdwood3cd04342012-03-09 12:02:08 +00002940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002941
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002942 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002943 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002944 update.kcontrol = kcontrol;
2945 update.reg = e->reg;
2946 update.mask = mask;
2947 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002948 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002949
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002950 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002951
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002952 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002953 }
2954
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002955 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002956 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957}
2958EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2959
2960/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002961 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2962 * @kcontrol: mixer control
2963 * @ucontrol: control element information
2964 *
2965 * Returns 0 for success.
2966 */
2967int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
2969{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002970 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002971 return 0;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2974
2975/**
2976 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2977 * @kcontrol: mixer control
2978 * @ucontrol: control element information
2979 *
2980 * Returns 0 for success.
2981 */
2982int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2984{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002985 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002986 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002987 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002988 struct soc_enum *e =
2989 (struct soc_enum *)kcontrol->private_value;
2990 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002991
2992 if (ucontrol->value.enumerated.item[0] >= e->max)
2993 return -EINVAL;
2994
Liam Girdwood3cd04342012-03-09 12:02:08 +00002995 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002996
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002997 value = ucontrol->value.enumerated.item[0];
2998 change = dapm_kcontrol_set_value(kcontrol, value);
2999 if (change)
3000 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003001
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003002 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003003 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003004}
3005EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3006
3007/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003008 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3009 * callback
3010 * @kcontrol: mixer control
3011 * @ucontrol: control element information
3012 *
3013 * Callback to get the value of a dapm semi enumerated double mixer control.
3014 *
3015 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3016 * used for handling bitfield coded enumeration for example.
3017 *
3018 * Returns 0 for success.
3019 */
3020int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
3022{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003023 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003024 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003025 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003026
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003027 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003028 val = (reg_val >> e->shift_l) & e->mask;
3029 for (mux = 0; mux < e->max; mux++) {
3030 if (val == e->values[mux])
3031 break;
3032 }
3033 ucontrol->value.enumerated.item[0] = mux;
3034 if (e->shift_l != e->shift_r) {
3035 val = (reg_val >> e->shift_r) & e->mask;
3036 for (mux = 0; mux < e->max; mux++) {
3037 if (val == e->values[mux])
3038 break;
3039 }
3040 ucontrol->value.enumerated.item[1] = mux;
3041 }
3042
3043 return 0;
3044}
3045EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3046
3047/**
3048 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3049 * callback
3050 * @kcontrol: mixer control
3051 * @ucontrol: control element information
3052 *
3053 * Callback to set the value of a dapm semi enumerated double mixer control.
3054 *
3055 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3056 * used for handling bitfield coded enumeration for example.
3057 *
3058 * Returns 0 for success.
3059 */
3060int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3061 struct snd_ctl_elem_value *ucontrol)
3062{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003063 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003064 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003065 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003066 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003067 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003068 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003069
3070 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3071 return -EINVAL;
3072 mux = ucontrol->value.enumerated.item[0];
3073 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3074 mask = e->mask << e->shift_l;
3075 if (e->shift_l != e->shift_r) {
3076 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3077 return -EINVAL;
3078 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3079 mask |= e->mask << e->shift_r;
3080 }
3081
Liam Girdwood3cd04342012-03-09 12:02:08 +00003082 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003083
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003084 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003085 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003086 update.kcontrol = kcontrol;
3087 update.reg = e->reg;
3088 update.mask = mask;
3089 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003090 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003091
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003092 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003093
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003094 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003095 }
3096
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003097 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003098 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003099}
3100EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3101
3102/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003103 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3104 *
3105 * @kcontrol: mixer control
3106 * @uinfo: control element information
3107 *
3108 * Callback to provide information about a pin switch control.
3109 */
3110int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *uinfo)
3112{
3113 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3114 uinfo->count = 1;
3115 uinfo->value.integer.min = 0;
3116 uinfo->value.integer.max = 1;
3117
3118 return 0;
3119}
3120EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3121
3122/**
3123 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3124 *
3125 * @kcontrol: mixer control
3126 * @ucontrol: Value
3127 */
3128int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol)
3130{
Mark Brown48a8c392012-02-14 17:11:15 -08003131 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003132 const char *pin = (const char *)kcontrol->private_value;
3133
Liam Girdwood3cd04342012-03-09 12:02:08 +00003134 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003135
3136 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003137 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003138
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003139 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003140
3141 return 0;
3142}
3143EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3144
3145/**
3146 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3147 *
3148 * @kcontrol: mixer control
3149 * @ucontrol: Value
3150 */
3151int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3152 struct snd_ctl_elem_value *ucontrol)
3153{
Mark Brown48a8c392012-02-14 17:11:15 -08003154 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003155 const char *pin = (const char *)kcontrol->private_value;
3156
Liam Girdwood3cd04342012-03-09 12:02:08 +00003157 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003158
3159 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003160 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003161 else
Mark Brown48a8c392012-02-14 17:11:15 -08003162 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003163
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003164 mutex_unlock(&card->dapm_mutex);
3165
Mark Brown48a8c392012-02-14 17:11:15 -08003166 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003167 return 0;
3168}
3169EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3170
Mark Brown5ba06fc2012-02-16 11:07:13 -08003171static struct snd_soc_dapm_widget *
3172snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3173 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003174{
3175 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003176 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003177
3178 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003179 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003180
Mark Brown62ea8742012-01-21 21:14:48 +00003181 switch (w->id) {
3182 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003183 w->regulator = devm_regulator_get(dapm->dev, w->name);
3184 if (IS_ERR(w->regulator)) {
3185 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003186 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003187 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003188 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003189 }
Mark Brown8784c772013-01-10 19:33:47 +00003190
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003191 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003192 ret = regulator_allow_bypass(w->regulator, true);
3193 if (ret != 0)
3194 dev_warn(w->dapm->dev,
3195 "ASoC: Failed to unbypass %s: %d\n",
3196 w->name, ret);
3197 }
Mark Brown62ea8742012-01-21 21:14:48 +00003198 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003199 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003200#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003201 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003202 if (IS_ERR(w->clk)) {
3203 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003204 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003205 w->name, ret);
3206 return NULL;
3207 }
Mark Brownec029952012-06-04 08:16:20 +01003208#else
3209 return NULL;
3210#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003211 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003212 default:
3213 break;
3214 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003215
Mark Brown88e8b9a2011-03-02 18:18:24 +00003216 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003217 w->name = kasprintf(GFP_KERNEL, "%s %s",
3218 dapm->codec->name_prefix, widget->name);
3219 else
3220 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3221
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003222 if (w->name == NULL) {
3223 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003224 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003225 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003226
Mark Brown7ca3a182011-10-08 14:04:50 +01003227 switch (w->id) {
3228 case snd_soc_dapm_switch:
3229 case snd_soc_dapm_mixer:
3230 case snd_soc_dapm_mixer_named_ctl:
3231 w->power_check = dapm_generic_check_power;
3232 break;
3233 case snd_soc_dapm_mux:
3234 case snd_soc_dapm_virt_mux:
3235 case snd_soc_dapm_value_mux:
3236 w->power_check = dapm_generic_check_power;
3237 break;
Mark Brown46162742013-06-05 19:36:11 +01003238 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003239 w->power_check = dapm_adc_check_power;
3240 break;
Mark Brown46162742013-06-05 19:36:11 +01003241 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003242 w->power_check = dapm_dac_check_power;
3243 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003244 case snd_soc_dapm_adc:
3245 case snd_soc_dapm_aif_out:
3246 case snd_soc_dapm_dac:
3247 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003248 case snd_soc_dapm_pga:
3249 case snd_soc_dapm_out_drv:
3250 case snd_soc_dapm_input:
3251 case snd_soc_dapm_output:
3252 case snd_soc_dapm_micbias:
3253 case snd_soc_dapm_spk:
3254 case snd_soc_dapm_hp:
3255 case snd_soc_dapm_mic:
3256 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003257 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003258 w->power_check = dapm_generic_check_power;
3259 break;
3260 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003261 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003262 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003263 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003264 w->power_check = dapm_supply_check_power;
3265 break;
3266 default:
3267 w->power_check = dapm_always_on_check_power;
3268 break;
3269 }
3270
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003271 w->dapm = dapm;
3272 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003273 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003274 INIT_LIST_HEAD(&w->sources);
3275 INIT_LIST_HEAD(&w->sinks);
3276 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003277 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003278 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003279
3280 /* machine layer set ups unconnected pins and insertions */
3281 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003282 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003283}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003284
3285/**
Mark Brown4ba13272008-05-13 14:51:19 +02003286 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003287 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003288 * @widget: widget array
3289 * @num: number of widgets
3290 *
3291 * Creates new DAPM controls based upon the templates.
3292 *
3293 * Returns 0 for success else error.
3294 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003295int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003296 const struct snd_soc_dapm_widget *widget,
3297 int num)
3298{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003299 struct snd_soc_dapm_widget *w;
3300 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003301 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003302
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003303 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003304 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003305 w = snd_soc_dapm_new_control(dapm, widget);
3306 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003307 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003308 "ASoC: Failed to create DAPM control %s\n",
3309 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003310 ret = -ENOMEM;
3311 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003312 }
Mark Brown4ba13272008-05-13 14:51:19 +02003313 widget++;
3314 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003315 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003316 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003317}
3318EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3319
Mark Brownc74184e2012-04-04 22:12:09 +01003320static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3321 struct snd_kcontrol *kcontrol, int event)
3322{
3323 struct snd_soc_dapm_path *source_p, *sink_p;
3324 struct snd_soc_dai *source, *sink;
3325 const struct snd_soc_pcm_stream *config = w->params;
3326 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003327 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003328 u64 fmt;
3329 int ret;
3330
3331 BUG_ON(!config);
3332 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3333
3334 /* We only support a single source and sink, pick the first */
3335 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3336 list_sink);
3337 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3338 list_source);
3339
3340 BUG_ON(!source_p || !sink_p);
3341 BUG_ON(!sink_p->source || !source_p->sink);
3342 BUG_ON(!source_p->source || !sink_p->sink);
3343
3344 source = source_p->source->priv;
3345 sink = sink_p->sink->priv;
3346
3347 /* Be a little careful as we don't want to overflow the mask array */
3348 if (config->formats) {
3349 fmt = ffs(config->formats) - 1;
3350 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003351 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003352 config->formats);
3353 fmt = 0;
3354 }
3355
3356 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003357 params = kzalloc(sizeof(*params), GFP_KERNEL);
3358 if (!params) {
3359 ret = -ENOMEM;
3360 goto out;
3361 }
3362 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003363
Mark Brown9747cec2012-04-26 19:12:21 +01003364 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003365 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003366 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003367 config->rate_max;
3368
Mark Brown9747cec2012-04-26 19:12:21 +01003369 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003370 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003371 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003372 = config->channels_max;
3373
3374 memset(&substream, 0, sizeof(substream));
3375
3376 switch (event) {
3377 case SND_SOC_DAPM_PRE_PMU:
3378 if (source->driver->ops && source->driver->ops->hw_params) {
3379 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3380 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003381 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003382 if (ret != 0) {
3383 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003384 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003385 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003386 }
3387 }
3388
3389 if (sink->driver->ops && sink->driver->ops->hw_params) {
3390 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003391 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003392 sink);
3393 if (ret != 0) {
3394 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003395 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003396 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003397 }
3398 }
3399 break;
3400
3401 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003402 ret = snd_soc_dai_digital_mute(sink, 0,
3403 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003404 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003405 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003406 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003407 break;
3408
3409 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003410 ret = snd_soc_dai_digital_mute(sink, 1,
3411 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003412 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003413 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003414 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003415 break;
3416
3417 default:
3418 BUG();
3419 return -EINVAL;
3420 }
3421
Mark Brown9747cec2012-04-26 19:12:21 +01003422out:
3423 kfree(params);
3424 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003425}
3426
3427int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3428 const struct snd_soc_pcm_stream *params,
3429 struct snd_soc_dapm_widget *source,
3430 struct snd_soc_dapm_widget *sink)
3431{
3432 struct snd_soc_dapm_route routes[2];
3433 struct snd_soc_dapm_widget template;
3434 struct snd_soc_dapm_widget *w;
3435 size_t len;
3436 char *link_name;
3437
3438 len = strlen(source->name) + strlen(sink->name) + 2;
3439 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3440 if (!link_name)
3441 return -ENOMEM;
3442 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3443
3444 memset(&template, 0, sizeof(template));
3445 template.reg = SND_SOC_NOPM;
3446 template.id = snd_soc_dapm_dai_link;
3447 template.name = link_name;
3448 template.event = snd_soc_dai_link_event;
3449 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3450 SND_SOC_DAPM_PRE_PMD;
3451
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003452 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003453
3454 w = snd_soc_dapm_new_control(&card->dapm, &template);
3455 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003456 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003457 link_name);
3458 return -ENOMEM;
3459 }
3460
3461 w->params = params;
3462
3463 memset(&routes, 0, sizeof(routes));
3464
3465 routes[0].source = source->name;
3466 routes[0].sink = link_name;
3467 routes[1].source = link_name;
3468 routes[1].sink = sink->name;
3469
3470 return snd_soc_dapm_add_routes(&card->dapm, routes,
3471 ARRAY_SIZE(routes));
3472}
3473
Mark Brown888df392012-02-16 19:37:51 -08003474int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3475 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003476{
Mark Brown888df392012-02-16 19:37:51 -08003477 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003478 struct snd_soc_dapm_widget *w;
3479
Mark Brown888df392012-02-16 19:37:51 -08003480 WARN_ON(dapm->dev != dai->dev);
3481
3482 memset(&template, 0, sizeof(template));
3483 template.reg = SND_SOC_NOPM;
3484
3485 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003486 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003487 template.name = dai->driver->playback.stream_name;
3488 template.sname = dai->driver->playback.stream_name;
3489
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003490 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003491 template.name);
3492
3493 w = snd_soc_dapm_new_control(dapm, &template);
3494 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003495 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003496 dai->driver->playback.stream_name);
3497 }
3498
3499 w->priv = dai;
3500 dai->playback_widget = w;
3501 }
3502
3503 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003504 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003505 template.name = dai->driver->capture.stream_name;
3506 template.sname = dai->driver->capture.stream_name;
3507
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003508 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003509 template.name);
3510
3511 w = snd_soc_dapm_new_control(dapm, &template);
3512 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003513 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003514 dai->driver->capture.stream_name);
3515 }
3516
3517 w->priv = dai;
3518 dai->capture_widget = w;
3519 }
3520
3521 return 0;
3522}
3523
3524int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3525{
3526 struct snd_soc_dapm_widget *dai_w, *w;
3527 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003528
3529 /* For each DAI widget... */
3530 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003531 switch (dai_w->id) {
3532 case snd_soc_dapm_dai_in:
3533 case snd_soc_dapm_dai_out:
3534 break;
3535 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003536 continue;
Mark Brown46162742013-06-05 19:36:11 +01003537 }
Mark Brown888df392012-02-16 19:37:51 -08003538
3539 dai = dai_w->priv;
3540
3541 /* ...find all widgets with the same stream and link them */
3542 list_for_each_entry(w, &card->widgets, list) {
3543 if (w->dapm != dai_w->dapm)
3544 continue;
3545
Mark Brown46162742013-06-05 19:36:11 +01003546 switch (w->id) {
3547 case snd_soc_dapm_dai_in:
3548 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003549 continue;
Mark Brown46162742013-06-05 19:36:11 +01003550 default:
3551 break;
3552 }
Mark Brown888df392012-02-16 19:37:51 -08003553
3554 if (!w->sname)
3555 continue;
3556
3557 if (dai->driver->playback.stream_name &&
3558 strstr(w->sname,
3559 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003560 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003561 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003562
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003563 snd_soc_dapm_add_path(w->dapm,
3564 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003565 }
3566
3567 if (dai->driver->capture.stream_name &&
3568 strstr(w->sname,
3569 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003570 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003571 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003572
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003573 snd_soc_dapm_add_path(w->dapm, w,
3574 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003575 }
3576 }
3577 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003578
Mark Brown888df392012-02-16 19:37:51 -08003579 return 0;
3580}
Liam Girdwood64a648c222011-07-25 11:15:15 +01003581
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003582static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3583 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003584{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003585
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003586 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3587 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3588 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003589
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003590 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3591 w_cpu = cpu_dai->playback_widget;
3592 w_codec = codec_dai->playback_widget;
3593 } else {
3594 w_cpu = cpu_dai->capture_widget;
3595 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003596 }
3597
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003598 if (w_cpu) {
3599
3600 dapm_mark_dirty(w_cpu, "stream event");
3601
3602 switch (event) {
3603 case SND_SOC_DAPM_STREAM_START:
3604 w_cpu->active = 1;
3605 break;
3606 case SND_SOC_DAPM_STREAM_STOP:
3607 w_cpu->active = 0;
3608 break;
3609 case SND_SOC_DAPM_STREAM_SUSPEND:
3610 case SND_SOC_DAPM_STREAM_RESUME:
3611 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3612 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3613 break;
3614 }
3615 }
3616
3617 if (w_codec) {
3618
3619 dapm_mark_dirty(w_codec, "stream event");
3620
3621 switch (event) {
3622 case SND_SOC_DAPM_STREAM_START:
3623 w_codec->active = 1;
3624 break;
3625 case SND_SOC_DAPM_STREAM_STOP:
3626 w_codec->active = 0;
3627 break;
3628 case SND_SOC_DAPM_STREAM_SUSPEND:
3629 case SND_SOC_DAPM_STREAM_RESUME:
3630 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3631 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3632 break;
3633 }
3634 }
3635
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003636 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003637}
3638
3639/**
3640 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3641 * @rtd: PCM runtime data
3642 * @stream: stream name
3643 * @event: stream event
3644 *
3645 * Sends a stream event to the dapm core. The core then makes any
3646 * necessary widget power changes.
3647 *
3648 * Returns 0 for success else error.
3649 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003650void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3651 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003652{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003653 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003654
Liam Girdwood3cd04342012-03-09 12:02:08 +00003655 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003656 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003657 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003658}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003659
3660/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003661 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003662 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003663 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003664 *
Mark Brown74b8f952009-06-06 11:26:15 +01003665 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003666 * a valid audio route and active audio stream.
3667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3668 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003670int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003671{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003672 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003673}
Liam Girdwooda5302182008-07-07 13:35:17 +01003674EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003675
3676/**
Mark Brownda341832010-03-15 19:23:37 +00003677 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003678 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003679 * @pin: pin name
3680 *
3681 * Enables input/output pin regardless of any other state. This is
3682 * intended for use with microphone bias supplies used in microphone
3683 * jack detection.
3684 *
3685 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3686 * do any widget power switching.
3687 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003688int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3689 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003690{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003691 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003692
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003693 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003694 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003695 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003696 }
3697
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003698 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003699 w->connected = 1;
3700 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003701 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003702
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003703 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003704}
3705EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3706
3707/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003708 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003709 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003710 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003711 *
Mark Brown74b8f952009-06-06 11:26:15 +01003712 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003713 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3714 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003715 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003716int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3717 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003718{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003719 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003720}
3721EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3722
3723/**
Mark Brown5817b522008-09-24 11:23:11 +01003724 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003725 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003726 * @pin: pin name
3727 *
3728 * Marks the specified pin as being not connected, disabling it along
3729 * any parent or child widgets. At present this is identical to
3730 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3731 * additional things such as disabling controls which only affect
3732 * paths through the pin.
3733 *
3734 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3735 * do any widget power switching.
3736 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003737int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003738{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003740}
3741EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3742
3743/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003744 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003745 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003746 * @pin: audio signal pin endpoint (or start point)
3747 *
3748 * Get audio pin status - connected or disconnected.
3749 *
3750 * Returns 1 for connected otherwise 0.
3751 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003752int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3753 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003754{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003755 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003756
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003757 if (w)
3758 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003759
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003760 return 0;
3761}
Liam Girdwooda5302182008-07-07 13:35:17 +01003762EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003763
3764/**
Mark Brown1547aba2010-05-07 21:11:40 +01003765 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003766 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003767 * @pin: audio signal pin endpoint (or start point)
3768 *
3769 * Mark the given endpoint or pin as ignoring suspend. When the
3770 * system is disabled a path between two endpoints flagged as ignoring
3771 * suspend will not be disabled. The path must already be enabled via
3772 * normal means at suspend time, it will not be turned on if it was not
3773 * already enabled.
3774 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003775int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3776 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003777{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003778 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003779
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003780 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003781 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003782 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003783 }
3784
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003785 w->ignore_suspend = 1;
3786
3787 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003788}
3789EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3790
Stephen Warren16332812011-11-23 12:42:04 -07003791static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3792 struct snd_soc_dapm_widget *w)
3793{
3794 struct snd_soc_dapm_path *p;
3795
3796 list_for_each_entry(p, &card->paths, list) {
3797 if ((p->source == w) || (p->sink == w)) {
3798 dev_dbg(card->dev,
3799 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3800 p->source->name, p->source->id, p->source->dapm,
3801 p->sink->name, p->sink->id, p->sink->dapm);
3802
3803 /* Connected to something other than the codec */
3804 if (p->source->dapm != p->sink->dapm)
3805 return true;
3806 /*
3807 * Loopback connection from codec external pin to
3808 * codec external pin
3809 */
3810 if (p->sink->id == snd_soc_dapm_input) {
3811 switch (p->source->id) {
3812 case snd_soc_dapm_output:
3813 case snd_soc_dapm_micbias:
3814 return true;
3815 default:
3816 break;
3817 }
3818 }
3819 }
3820 }
3821
3822 return false;
3823}
3824
3825/**
3826 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3827 * @codec: The codec whose pins should be processed
3828 *
3829 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3830 * which are unused. Pins are used if they are connected externally to the
3831 * codec, whether that be to some other device, or a loop-back connection to
3832 * the codec itself.
3833 */
3834void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3835{
3836 struct snd_soc_card *card = codec->card;
3837 struct snd_soc_dapm_context *dapm = &codec->dapm;
3838 struct snd_soc_dapm_widget *w;
3839
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003840 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003841 &card->dapm, &codec->dapm);
3842
3843 list_for_each_entry(w, &card->widgets, list) {
3844 if (w->dapm != dapm)
3845 continue;
3846 switch (w->id) {
3847 case snd_soc_dapm_input:
3848 case snd_soc_dapm_output:
3849 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003850 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003851 w->name);
3852 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003853 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003854 "... Not in map; disabling\n");
3855 snd_soc_dapm_nc_pin(dapm, w->name);
3856 }
3857 break;
3858 default:
3859 break;
3860 }
3861 }
3862}
3863
Mark Brown1547aba2010-05-07 21:11:40 +01003864/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003865 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003866 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003867 *
3868 * Free all dapm widgets and resources.
3869 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003870void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003871{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003872 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003873 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003874 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003875 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003876}
3877EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3878
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003879static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003880{
Liam Girdwood01005a72012-07-06 16:57:05 +01003881 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003882 struct snd_soc_dapm_widget *w;
3883 LIST_HEAD(down_list);
3884 int powerdown = 0;
3885
Liam Girdwood01005a72012-07-06 16:57:05 +01003886 mutex_lock(&card->dapm_mutex);
3887
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003888 list_for_each_entry(w, &dapm->card->widgets, list) {
3889 if (w->dapm != dapm)
3890 continue;
Mark Brown51737472009-06-22 13:16:51 +01003891 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003892 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003893 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003894 powerdown = 1;
3895 }
3896 }
3897
3898 /* If there were no widgets to power down we're already in
3899 * standby.
3900 */
3901 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003902 if (dapm->bias_level == SND_SOC_BIAS_ON)
3903 snd_soc_dapm_set_bias_level(dapm,
3904 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003905 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003906 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3907 snd_soc_dapm_set_bias_level(dapm,
3908 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003909 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003910
3911 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003912}
Mark Brown51737472009-06-22 13:16:51 +01003913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003914/*
3915 * snd_soc_dapm_shutdown - callback for system shutdown
3916 */
3917void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3918{
3919 struct snd_soc_codec *codec;
3920
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003921 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003922 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003923 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3924 snd_soc_dapm_set_bias_level(&codec->dapm,
3925 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003926 }
Mark Brown51737472009-06-22 13:16:51 +01003927}
3928
Richard Purdie2b97eab2006-10-06 18:32:18 +02003929/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003930MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003931MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3932MODULE_LICENSE("GPL");