blob: 79b836c1045da06eb7cb2db9b154314bbb178241 [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
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Mark Brown452c5ea2009-05-17 21:41:23 +0100127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800129 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
Mark Browned5a4c42011-02-18 11:12:42 -0800136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Browned5a4c42011-02-18 11:12:42 -0800139 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100140 int ret = 0;
141
Mark Brownf83fba82009-05-18 15:44:43 +0100142 switch (level) {
143 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100154 break;
155 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100157 return -EINVAL;
158 }
159
Mark Brown84e90932010-11-04 00:07:02 -0400160 trace_snd_soc_bias_level_start(card, level);
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100163 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100167 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100169 }
Mark Brown1badabd2010-12-04 12:41:04 +0000170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100174
Mark Brown84e90932010-11-04 00:07:02 -0400175 trace_snd_soc_bias_level_done(card, level);
176
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 return ret;
178}
179
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600190 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100193 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
199
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
204 }
205 break;
206 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600207 struct soc_enum *e = (struct soc_enum *)
208 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200209 int val, item, bitmask;
210
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100211 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200212 ;
213 val = snd_soc_read(w->codec, e->reg);
214 item = (val >> e->shift_l) & (bitmask - 1);
215
216 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100217 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200218 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 p->connect = 1;
220 }
221 }
222 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000223 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600224 struct soc_enum *e = (struct soc_enum *)
225 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000226
227 p->connect = 0;
228 /* since a virtual mux has no backing registers to
229 * decide which path to connect, it will try to match
230 * with the first enumeration. This is to ensure
231 * that the default mux choice (the first) will be
232 * correctly powered up during initialization.
233 */
234 if (!strcmp(p->name, e->texts[0]))
235 p->connect = 1;
236 }
237 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200238 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200239 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600240 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200241 int val, item;
242
243 val = snd_soc_read(w->codec, e->reg);
244 val = (val >> e->shift_l) & e->mask;
245 for (item = 0; item < e->max; item++) {
246 if (val == e->values[item])
247 break;
248 }
249
250 p->connect = 0;
251 for (i = 0; i < e->max; i++) {
252 if (!(strcmp(p->name, e->texts[i])) && item == i)
253 p->connect = 1;
254 }
255 }
256 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 /* does not effect routing - always connected */
258 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600259 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200260 case snd_soc_dapm_output:
261 case snd_soc_dapm_adc:
262 case snd_soc_dapm_input:
263 case snd_soc_dapm_dac:
264 case snd_soc_dapm_micbias:
265 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100266 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100267 case snd_soc_dapm_aif_in:
268 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200269 p->connect = 1;
270 break;
271 /* does effect routing - dynamically connected */
272 case snd_soc_dapm_hp:
273 case snd_soc_dapm_mic:
274 case snd_soc_dapm_spk:
275 case snd_soc_dapm_line:
276 case snd_soc_dapm_pre:
277 case snd_soc_dapm_post:
278 p->connect = 0;
279 break;
280 }
281}
282
Mark Brown74b8f952009-06-06 11:26:15 +0100283/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200284static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200285 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
286 struct snd_soc_dapm_path *path, const char *control_name,
287 const struct snd_kcontrol_new *kcontrol)
288{
289 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 int i;
291
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100292 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200294 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295 list_add(&path->list_sink, &dest->sources);
296 list_add(&path->list_source, &src->sinks);
297 path->name = (char*)e->texts[i];
298 dapm_set_path_status(dest, path, 0);
299 return 0;
300 }
301 }
302
303 return -ENODEV;
304}
305
Mark Brown74b8f952009-06-06 11:26:15 +0100306/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200307static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200308 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
309 struct snd_soc_dapm_path *path, const char *control_name)
310{
311 int i;
312
313 /* search for mixer kcontrol */
314 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600315 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200316 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 list_add(&path->list_sink, &dest->sources);
318 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600319 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200320 dapm_set_path_status(dest, path, i);
321 return 0;
322 }
323 }
324 return -ENODEV;
325}
326
Richard Purdie2b97eab2006-10-06 18:32:18 +0200327/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200328static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 struct snd_soc_dapm_widget *w)
330{
331 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000332 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000334 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000335 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600336 struct snd_soc_dapm_widget_list *wlist;
337 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000338
339 if (dapm->codec)
340 prefix = dapm->codec->name_prefix;
341 else
342 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000344 if (prefix)
345 prefix_len = strlen(prefix) + 1;
346 else
347 prefix_len = 0;
348
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349 /* add kcontrol */
350 for (i = 0; i < w->num_kcontrols; i++) {
351
352 /* match name */
353 list_for_each_entry(path, &w->sources, list_sink) {
354
355 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600356 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 continue;
358
Stephen Warrenfafd2172011-04-28 17:38:00 -0600359 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
360 sizeof(struct snd_soc_dapm_widget *),
361 wlist = kzalloc(wlistsize, GFP_KERNEL);
362 if (wlist == NULL) {
363 dev_err(dapm->dev,
364 "asoc: can't allocate widget list for %s\n",
365 w->name);
366 return -ENOMEM;
367 }
368 wlist->num_widgets = 1;
369 wlist->widgets[0] = w;
370
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000371 /* add dapm control with long name.
372 * for dapm_mixer this is the concatenation of the
373 * mixer and kcontrol name.
374 * for dapm_mixer_named_ctl this is simply the
375 * kcontrol name.
376 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600377 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000378 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000379 name_len += 1 + strlen(w->name);
380
Mark Brown219b93f2008-10-28 13:02:31 +0000381 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382
Stephen Warrenfafd2172011-04-28 17:38:00 -0600383 if (path->long_name == NULL) {
384 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200385 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600386 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000388 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000389 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000390 /* The control will get a prefix from
391 * the control creation process but
392 * we're also using the same prefix
393 * for widgets so cut the prefix off
394 * the front of the widget name.
395 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000396 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000397 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600398 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000399 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000400 case snd_soc_dapm_mixer_named_ctl:
401 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600402 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000403 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000404 }
405
Mark Brown219b93f2008-10-28 13:02:31 +0000406 path->long_name[name_len - 1] = '\0';
407
Stephen Warrenfafd2172011-04-28 17:38:00 -0600408 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
409 wlist, path->long_name,
410 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200411 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200413 dev_err(dapm->dev,
414 "asoc: failed to add dapm kcontrol %s: %d\n",
415 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600416 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 kfree(path->long_name);
418 path->long_name = NULL;
419 return ret;
420 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600421 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 }
423 }
424 return ret;
425}
426
427/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200428static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429 struct snd_soc_dapm_widget *w)
430{
431 struct snd_soc_dapm_path *path = NULL;
432 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000433 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000434 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000435 size_t prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600437 struct snd_soc_dapm_widget_list *wlist;
438 size_t wlistsize;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200439
440 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200441 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 return -EINVAL;
443 }
444
Stephen Warrenfafd2172011-04-28 17:38:00 -0600445 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
446 sizeof(struct snd_soc_dapm_widget *),
447 wlist = kzalloc(wlistsize, GFP_KERNEL);
448 if (wlist == NULL) {
449 dev_err(dapm->dev,
450 "asoc: can't allocate widget list for %s\n", w->name);
451 return -ENOMEM;
452 }
453 wlist->num_widgets = 1;
454 wlist->widgets[0] = w;
455
Mark Brownefb7ac32011-03-08 17:23:24 +0000456 if (dapm->codec)
457 prefix = dapm->codec->name_prefix;
458 else
459 prefix = NULL;
460
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000461 if (prefix)
462 prefix_len = strlen(prefix) + 1;
463 else
464 prefix_len = 0;
465
466 /* The control will get a prefix from the control creation
467 * process but we're also using the same prefix for widgets so
468 * cut the prefix off the front of the widget name.
469 */
Stephen Warrenfafd2172011-04-28 17:38:00 -0600470 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
471 w->name + prefix_len, prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200472 ret = snd_ctl_add(card, kcontrol);
473
Richard Purdie2b97eab2006-10-06 18:32:18 +0200474 if (ret < 0)
475 goto err;
476
Stephen Warrenfad59882011-04-28 17:37:59 -0600477 w->kcontrols[0] = kcontrol;
478
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479 list_for_each_entry(path, &w->sources, list_sink)
480 path->kcontrol = kcontrol;
481
482 return ret;
483
484err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200485 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600486 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200487 return ret;
488}
489
490/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200491static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492 struct snd_soc_dapm_widget *w)
493{
Mark Browna6c65732010-03-03 17:45:21 +0000494 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200495 dev_err(w->dapm->dev,
496 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497
Mark Browna6c65732010-03-03 17:45:21 +0000498 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499}
500
501/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200502static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503{
504 struct snd_soc_dapm_path *p;
505
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200506 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507 p->walked = 0;
508}
509
Mark Brown9949788b2010-05-07 20:24:05 +0100510/* We implement power down on suspend by checking the power state of
511 * the ALSA card - when we are suspending the ALSA state for the card
512 * is set to D3.
513 */
514static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
515{
Mark Brown12ea2c72011-03-02 18:17:32 +0000516 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100519 case SNDRV_CTL_POWER_D3hot:
520 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100521 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200522 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
523 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100524 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100525 default:
526 return 1;
527 }
528}
529
Richard Purdie2b97eab2006-10-06 18:32:18 +0200530/*
531 * Recursively check for a completed path to an active or physically connected
532 * output widget. Returns number of complete paths.
533 */
534static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
535{
536 struct snd_soc_dapm_path *path;
537 int con = 0;
538
Mark Brown246d0a12009-04-22 18:24:55 +0100539 if (widget->id == snd_soc_dapm_supply)
540 return 0;
541
Mark Brown010ff262009-08-17 17:39:22 +0100542 switch (widget->id) {
543 case snd_soc_dapm_adc:
544 case snd_soc_dapm_aif_out:
545 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100546 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100547 default:
548 break;
549 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550
551 if (widget->connected) {
552 /* connected pin ? */
553 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100554 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200555
556 /* connected jack or spk ? */
557 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300558 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown9949788b2010-05-07 20:24:05 +0100559 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560 }
561
562 list_for_each_entry(path, &widget->sinks, list_source) {
563 if (path->walked)
564 continue;
565
566 if (path->sink && path->connect) {
567 path->walked = 1;
568 con += is_connected_output_ep(path->sink);
569 }
570 }
571
572 return con;
573}
574
575/*
576 * Recursively check for a completed path to an active or physically connected
577 * input widget. Returns number of complete paths.
578 */
579static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
580{
581 struct snd_soc_dapm_path *path;
582 int con = 0;
583
Mark Brown246d0a12009-04-22 18:24:55 +0100584 if (widget->id == snd_soc_dapm_supply)
585 return 0;
586
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100588 switch (widget->id) {
589 case snd_soc_dapm_dac:
590 case snd_soc_dapm_aif_in:
591 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100592 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100593 default:
594 break;
595 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200596
597 if (widget->connected) {
598 /* connected pin ? */
599 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100600 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200601
602 /* connected VMID/Bias for lower pops */
603 if (widget->id == snd_soc_dapm_vmid)
Mark Brown9949788b2010-05-07 20:24:05 +0100604 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200605
606 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300607 if (widget->id == snd_soc_dapm_mic ||
608 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown9949788b2010-05-07 20:24:05 +0100609 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200610 }
611
612 list_for_each_entry(path, &widget->sources, list_sink) {
613 if (path->walked)
614 continue;
615
616 if (path->source && path->connect) {
617 path->walked = 1;
618 con += is_connected_input_ep(path->source);
619 }
620 }
621
622 return con;
623}
624
625/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300626 * Handler for generic register modifier widget.
627 */
628int dapm_reg_event(struct snd_soc_dapm_widget *w,
629 struct snd_kcontrol *kcontrol, int event)
630{
631 unsigned int val;
632
633 if (SND_SOC_DAPM_EVENT_ON(event))
634 val = w->on_val;
635 else
636 val = w->off_val;
637
638 snd_soc_update_bits(w->codec, -(w->reg + 1),
639 w->mask << w->shift, val << w->shift);
640
641 return 0;
642}
Mark Brown11589412008-07-29 11:42:23 +0100643EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300644
Mark Browncd0f2d42009-04-20 16:56:59 +0100645/* Generic check to see if a widget should be powered.
646 */
647static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
648{
649 int in, out;
650
651 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200652 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100653 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200654 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100655 return out != 0 && in != 0;
656}
657
Mark Brown6ea31b92009-04-20 17:15:41 +0100658/* Check to see if an ADC has power */
659static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
660{
661 int in;
662
663 if (w->active) {
664 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200665 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100666 return in != 0;
667 } else {
668 return dapm_generic_check_power(w);
669 }
670}
671
672/* Check to see if a DAC has power */
673static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
674{
675 int out;
676
677 if (w->active) {
678 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200679 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100680 return out != 0;
681 } else {
682 return dapm_generic_check_power(w);
683 }
684}
685
Mark Brown246d0a12009-04-22 18:24:55 +0100686/* Check to see if a power supply is needed */
687static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
688{
689 struct snd_soc_dapm_path *path;
690 int power = 0;
691
692 /* Check if one of our outputs is connected */
693 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100694 if (path->connected &&
695 !path->connected(path->source, path->sink))
696 continue;
697
Mark Brown30173582011-02-11 11:42:19 +0000698 if (!path->sink)
699 continue;
700
701 if (path->sink->force) {
702 power = 1;
703 break;
704 }
705
706 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100707 path->sink->power_check(path->sink)) {
708 power = 1;
709 break;
710 }
711 }
712
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200713 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100714
715 return power;
716}
717
Mark Brown38357ab2009-06-06 19:03:23 +0100718static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
719 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000720 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000721{
Mark Brown828a8422011-01-15 13:14:30 +0000722 int *sort;
723
724 if (power_up)
725 sort = dapm_up_seq;
726 else
727 sort = dapm_down_seq;
728
Mark Brown38357ab2009-06-06 19:03:23 +0100729 if (sort[a->id] != sort[b->id])
730 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000731 if (a->subseq != b->subseq) {
732 if (power_up)
733 return a->subseq - b->subseq;
734 else
735 return b->subseq - a->subseq;
736 }
Mark Brownb22ead22009-06-07 12:51:26 +0100737 if (a->reg != b->reg)
738 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000739 if (a->dapm != b->dapm)
740 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000741
Mark Brown38357ab2009-06-06 19:03:23 +0100742 return 0;
743}
Mark Brown42aa3412009-03-01 19:21:10 +0000744
Mark Brown38357ab2009-06-06 19:03:23 +0100745/* Insert a widget in order into a DAPM power sequence. */
746static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
747 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000748 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100749{
750 struct snd_soc_dapm_widget *w;
751
752 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000753 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100754 list_add_tail(&new_widget->power_list, &w->power_list);
755 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000756 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100757
Mark Brown38357ab2009-06-06 19:03:23 +0100758 list_add_tail(&new_widget->power_list, list);
759}
Mark Brown42aa3412009-03-01 19:21:10 +0000760
Mark Brown68f89ad2010-11-03 23:51:49 -0400761static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
762 struct snd_soc_dapm_widget *w, int event)
763{
764 struct snd_soc_card *card = dapm->card;
765 const char *ev_name;
766 int power, ret;
767
768 switch (event) {
769 case SND_SOC_DAPM_PRE_PMU:
770 ev_name = "PRE_PMU";
771 power = 1;
772 break;
773 case SND_SOC_DAPM_POST_PMU:
774 ev_name = "POST_PMU";
775 power = 1;
776 break;
777 case SND_SOC_DAPM_PRE_PMD:
778 ev_name = "PRE_PMD";
779 power = 0;
780 break;
781 case SND_SOC_DAPM_POST_PMD:
782 ev_name = "POST_PMD";
783 power = 0;
784 break;
785 default:
786 BUG();
787 return;
788 }
789
790 if (w->power != power)
791 return;
792
793 if (w->event && (w->event_flags & event)) {
794 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
795 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400796 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400797 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400798 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400799 if (ret < 0)
800 pr_err("%s: %s event failed: %d\n",
801 ev_name, w->name, ret);
802 }
803}
804
Mark Brownb22ead22009-06-07 12:51:26 +0100805/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200806static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100807 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100808{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200809 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400810 struct snd_soc_dapm_widget *w;
811 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100812 unsigned int value = 0;
813 unsigned int mask = 0;
814 unsigned int cur_mask;
815
816 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
817 power_list)->reg;
818
819 list_for_each_entry(w, pending, power_list) {
820 cur_mask = 1 << w->shift;
821 BUG_ON(reg != w->reg);
822
823 if (w->invert)
824 power = !w->power;
825 else
826 power = w->power;
827
828 mask |= cur_mask;
829 if (power)
830 value |= cur_mask;
831
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200832 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100833 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
834 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100835
Mark Brown68f89ad2010-11-03 23:51:49 -0400836 /* Check for events */
837 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
838 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100839 }
840
Mark Brown81628102009-06-07 13:21:24 +0100841 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200842 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100843 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200844 value, mask, reg, card->pop_time);
845 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200846 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100847 }
848
849 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400850 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
851 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000852 }
Mark Brown42aa3412009-03-01 19:21:10 +0000853}
854
Mark Brownb22ead22009-06-07 12:51:26 +0100855/* Apply a DAPM power sequence.
856 *
857 * We walk over a pre-sorted list of widgets to apply power to. In
858 * order to minimise the number of writes to the device required
859 * multiple widgets will be updated in a single write where possible.
860 * Currently anything that requires more than a single write is not
861 * handled.
862 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200863static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000864 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100865{
866 struct snd_soc_dapm_widget *w, *n;
867 LIST_HEAD(pending);
868 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000869 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100870 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200871 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000872 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000873 int *sort;
874
875 if (power_up)
876 sort = dapm_up_seq;
877 else
878 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100879
Mark Brownb22ead22009-06-07 12:51:26 +0100880 list_for_each_entry_safe(w, n, list, power_list) {
881 ret = 0;
882
883 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200884 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000885 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100886 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200887 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100888
Mark Brown474b62d2011-01-18 16:14:44 +0000889 if (cur_dapm && cur_dapm->seq_notifier) {
890 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
891 if (sort[i] == cur_sort)
892 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000893 i,
894 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000895 }
896
Mark Brownb22ead22009-06-07 12:51:26 +0100897 INIT_LIST_HEAD(&pending);
898 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000899 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100900 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200901 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100902 }
903
Mark Brown163cac02009-06-07 10:12:52 +0100904 switch (w->id) {
905 case snd_soc_dapm_pre:
906 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100907 list_for_each_entry_safe_continue(w, n, list,
908 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100909
Mark Brownb22ead22009-06-07 12:51:26 +0100910 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100911 ret = w->event(w,
912 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100913 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100914 ret = w->event(w,
915 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100916 break;
917
918 case snd_soc_dapm_post:
919 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100920 list_for_each_entry_safe_continue(w, n, list,
921 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100922
Mark Brownb22ead22009-06-07 12:51:26 +0100923 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100924 ret = w->event(w,
925 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100926 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100927 ret = w->event(w,
928 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100929 break;
930
Mark Brown163cac02009-06-07 10:12:52 +0100931 default:
Mark Brown81628102009-06-07 13:21:24 +0100932 /* Queue it up for application */
933 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000934 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100935 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200936 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100937 list_move(&w->power_list, &pending);
938 break;
Mark Brown163cac02009-06-07 10:12:52 +0100939 }
Mark Brownb22ead22009-06-07 12:51:26 +0100940
941 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200942 dev_err(w->dapm->dev,
943 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100944 }
Mark Brownb22ead22009-06-07 12:51:26 +0100945
946 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +0000947 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000948
949 if (cur_dapm && cur_dapm->seq_notifier) {
950 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
951 if (sort[i] == cur_sort)
952 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000953 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000954 }
Mark Brown163cac02009-06-07 10:12:52 +0100955}
956
Mark Brown97404f22010-12-14 16:13:57 +0000957static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
958{
959 struct snd_soc_dapm_update *update = dapm->update;
960 struct snd_soc_dapm_widget *w;
961 int ret;
962
963 if (!update)
964 return;
965
966 w = update->widget;
967
968 if (w->event &&
969 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
970 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
971 if (ret != 0)
972 pr_err("%s DAPM pre-event failed: %d\n",
973 w->name, ret);
974 }
975
976 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
977 update->val);
978 if (ret < 0)
979 pr_err("%s DAPM update failed: %d\n", w->name, ret);
980
981 if (w->event &&
982 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
983 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
984 if (ret != 0)
985 pr_err("%s DAPM post-event failed: %d\n",
986 w->name, ret);
987 }
988}
989
Mark Brown9d0624a2011-02-18 11:49:43 -0800990/* Async callback run prior to DAPM sequences - brings to _PREPARE if
991 * they're changing state.
992 */
993static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
994{
995 struct snd_soc_dapm_context *d = data;
996 int ret;
Mark Brown97404f22010-12-14 16:13:57 +0000997
Mark Brown9d0624a2011-02-18 11:49:43 -0800998 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
999 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1000 if (ret != 0)
1001 dev_err(d->dev,
1002 "Failed to turn on bias: %d\n", ret);
1003 }
1004
1005 /* If we're changing to all on or all off then prepare */
1006 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1007 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1008 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1009 if (ret != 0)
1010 dev_err(d->dev,
1011 "Failed to prepare bias: %d\n", ret);
1012 }
1013}
1014
1015/* Async callback run prior to DAPM sequences - brings to their final
1016 * state.
1017 */
1018static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1019{
1020 struct snd_soc_dapm_context *d = data;
1021 int ret;
1022
1023 /* If we just powered the last thing off drop to standby bias */
1024 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1025 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1026 if (ret != 0)
1027 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1028 ret);
1029 }
1030
1031 /* If we're in standby and can support bias off then do that */
1032 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1033 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1034 if (ret != 0)
1035 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1036 }
1037
1038 /* If we just powered up then move to active bias */
1039 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1040 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1041 if (ret != 0)
1042 dev_err(d->dev, "Failed to apply active bias: %d\n",
1043 ret);
1044 }
1045}
Mark Brown97404f22010-12-14 16:13:57 +00001046
Mark Brown42aa3412009-03-01 19:21:10 +00001047/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001048 * Scan each dapm widget for complete audio path.
1049 * A complete path is a route that has valid endpoints i.e.:-
1050 *
1051 * o DAC to output pin.
1052 * o Input Pin to ADC.
1053 * o Input pin to Output pin (bypass, sidetone)
1054 * o DAC to ADC (loopback).
1055 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001056static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001057{
Mark Brown12ea2c72011-03-02 18:17:32 +00001058 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001059 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001060 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001061 LIST_HEAD(up_list);
1062 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001063 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001064 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001065
Mark Brown84e90932010-11-04 00:07:02 -04001066 trace_snd_soc_dapm_start(card);
1067
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001068 list_for_each_entry(d, &card->dapm_list, list)
1069 if (d->n_widgets)
1070 d->dev_power = 0;
1071
Mark Brown6d3ddc82009-05-16 17:47:29 +01001072 /* Check which widgets we need to power and store them in
1073 * lists indicating if they should be powered up or down.
1074 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001075 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001076 switch (w->id) {
1077 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001078 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001079 break;
1080 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001081 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001082 break;
1083
1084 default:
1085 if (!w->power_check)
1086 continue;
1087
Mark Brown9949788b2010-05-07 20:24:05 +01001088 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001089 power = w->power_check(w);
Mark Brown9949788b2010-05-07 20:24:05 +01001090 else
1091 power = 1;
1092 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001093 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001094
Mark Brown6d3ddc82009-05-16 17:47:29 +01001095 if (w->power == power)
1096 continue;
1097
Mark Brown84e90932010-11-04 00:07:02 -04001098 trace_snd_soc_dapm_widget_power(w, power);
1099
Mark Brown6d3ddc82009-05-16 17:47:29 +01001100 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001101 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001102 else
Mark Brown828a8422011-01-15 13:14:30 +00001103 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001104
1105 w->power = power;
1106 break;
1107 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001108 }
1109
Mark Brownb14b76a52009-08-17 11:55:38 +01001110 /* If there are no DAPM widgets then try to figure out power from the
1111 * event type.
1112 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001113 if (!dapm->n_widgets) {
Mark Brownb14b76a52009-08-17 11:55:38 +01001114 switch (event) {
1115 case SND_SOC_DAPM_STREAM_START:
1116 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001117 dapm->dev_power = 1;
Mark Brownb14b76a52009-08-17 11:55:38 +01001118 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001119 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001120 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001121 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001122 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001123 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001124 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001125 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001126 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001127 case SND_SOC_BIAS_STANDBY:
1128 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001129 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001130 break;
1131 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001132 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001133 break;
1134 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001135 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001136 default:
1137 break;
1138 }
1139 }
1140
Mark Brown52ba67b2011-04-04 21:05:11 +09001141 /* Force all contexts in the card to the same bias state */
1142 power = 0;
1143 list_for_each_entry(d, &card->dapm_list, list)
1144 if (d->dev_power)
1145 power = 1;
1146 list_for_each_entry(d, &card->dapm_list, list)
1147 d->dev_power = power;
1148
1149
Mark Brown9d0624a2011-02-18 11:49:43 -08001150 /* Run all the bias changes in parallel */
1151 list_for_each_entry(d, &dapm->card->dapm_list, list)
1152 async_schedule_domain(dapm_pre_sequence_async, d,
1153 &async_domain);
1154 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001155
Mark Brown6d3ddc82009-05-16 17:47:29 +01001156 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001157 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001158
Mark Brown97404f22010-12-14 16:13:57 +00001159 dapm_widget_update(dapm);
1160
Mark Brown6d3ddc82009-05-16 17:47:29 +01001161 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001162 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001163
Mark Brown9d0624a2011-02-18 11:49:43 -08001164 /* Run all the bias changes in parallel */
1165 list_for_each_entry(d, &dapm->card->dapm_list, list)
1166 async_schedule_domain(dapm_post_sequence_async, d,
1167 &async_domain);
1168 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001169
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001170 pop_dbg(dapm->dev, card->pop_time,
1171 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001172 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001173
Mark Brown84e90932010-11-04 00:07:02 -04001174 trace_snd_soc_dapm_done(card);
1175
Mark Brown42aa3412009-03-01 19:21:10 +00001176 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001177}
1178
Mark Brown79fb9382009-08-21 16:38:13 +01001179#ifdef CONFIG_DEBUG_FS
1180static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1181{
1182 file->private_data = inode->i_private;
1183 return 0;
1184}
1185
1186static ssize_t dapm_widget_power_read_file(struct file *file,
1187 char __user *user_buf,
1188 size_t count, loff_t *ppos)
1189{
1190 struct snd_soc_dapm_widget *w = file->private_data;
1191 char *buf;
1192 int in, out;
1193 ssize_t ret;
1194 struct snd_soc_dapm_path *p = NULL;
1195
1196 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1197 if (!buf)
1198 return -ENOMEM;
1199
1200 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001201 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001202 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001203 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001204
Mark Brownd033c362009-12-04 15:25:56 +00001205 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001206 w->name, w->power ? "On" : "Off", in, out);
1207
Mark Brownd033c362009-12-04 15:25:56 +00001208 if (w->reg >= 0)
1209 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1210 " - R%d(0x%x) bit %d",
1211 w->reg, w->reg, w->shift);
1212
1213 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1214
Mark Brown3eef08b2009-09-14 16:49:00 +01001215 if (w->sname)
1216 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1217 w->sname,
1218 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001219
1220 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001221 if (p->connected && !p->connected(w, p->sink))
1222 continue;
1223
Mark Brown79fb9382009-08-21 16:38:13 +01001224 if (p->connect)
1225 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001226 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001227 p->name ? p->name : "static",
1228 p->source->name);
1229 }
1230 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001231 if (p->connected && !p->connected(w, p->sink))
1232 continue;
1233
Mark Brown79fb9382009-08-21 16:38:13 +01001234 if (p->connect)
1235 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001236 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001237 p->name ? p->name : "static",
1238 p->sink->name);
1239 }
1240
1241 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1242
1243 kfree(buf);
1244 return ret;
1245}
1246
1247static const struct file_operations dapm_widget_power_fops = {
1248 .open = dapm_widget_power_open_file,
1249 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001250 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001251};
1252
Mark Brownef49e4f2011-04-04 20:48:13 +09001253static int dapm_bias_open_file(struct inode *inode, struct file *file)
1254{
1255 file->private_data = inode->i_private;
1256 return 0;
1257}
1258
1259static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1260 size_t count, loff_t *ppos)
1261{
1262 struct snd_soc_dapm_context *dapm = file->private_data;
1263 char *level;
1264
1265 switch (dapm->bias_level) {
1266 case SND_SOC_BIAS_ON:
1267 level = "On\n";
1268 break;
1269 case SND_SOC_BIAS_PREPARE:
1270 level = "Prepare\n";
1271 break;
1272 case SND_SOC_BIAS_STANDBY:
1273 level = "Standby\n";
1274 break;
1275 case SND_SOC_BIAS_OFF:
1276 level = "Off\n";
1277 break;
1278 default:
1279 BUG();
1280 level = "Unknown\n";
1281 break;
1282 }
1283
1284 return simple_read_from_buffer(user_buf, count, ppos, level,
1285 strlen(level));
1286}
1287
1288static const struct file_operations dapm_bias_fops = {
1289 .open = dapm_bias_open_file,
1290 .read = dapm_bias_read_file,
1291 .llseek = default_llseek,
1292};
1293
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001294void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1295 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001296{
Mark Brown79fb9382009-08-21 16:38:13 +01001297 struct dentry *d;
1298
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001299 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1300
1301 if (!dapm->debugfs_dapm) {
1302 printk(KERN_WARNING
1303 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001304 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001305 }
Mark Brown79fb9382009-08-21 16:38:13 +01001306
Mark Brownef49e4f2011-04-04 20:48:13 +09001307 d = debugfs_create_file("bias_level", 0444,
1308 dapm->debugfs_dapm, dapm,
1309 &dapm_bias_fops);
1310 if (!d)
1311 dev_warn(dapm->dev,
1312 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001313}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001314
1315static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1316{
1317 struct snd_soc_dapm_context *dapm = w->dapm;
1318 struct dentry *d;
1319
1320 if (!dapm->debugfs_dapm || !w->name)
1321 return;
1322
1323 d = debugfs_create_file(w->name, 0444,
1324 dapm->debugfs_dapm, w,
1325 &dapm_widget_power_fops);
1326 if (!d)
1327 dev_warn(w->dapm->dev,
1328 "ASoC: Failed to create %s debugfs file\n",
1329 w->name);
1330}
1331
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001332static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1333{
1334 debugfs_remove_recursive(dapm->debugfs_dapm);
1335}
1336
Mark Brown79fb9382009-08-21 16:38:13 +01001337#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001338void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1339 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001340{
1341}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001342
1343static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1344{
1345}
1346
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001347static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1348{
1349}
1350
Mark Brown79fb9382009-08-21 16:38:13 +01001351#endif
1352
Richard Purdie2b97eab2006-10-06 18:32:18 +02001353/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001354static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001355 struct snd_kcontrol *kcontrol, int change,
1356 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357{
1358 struct snd_soc_dapm_path *path;
1359 int found = 0;
1360
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001361 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001362 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001363 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001364 return -ENODEV;
1365
Mark Brown3a655772009-10-05 17:23:30 +01001366 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001367 return 0;
1368
1369 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001370 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001371 if (path->kcontrol != kcontrol)
1372 continue;
1373
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001374 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001375 continue;
1376
1377 found = 1;
1378 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001379 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380 path->connect = 1; /* new connection */
1381 else
1382 path->connect = 0; /* old connection must be powered down */
1383 }
1384
Mark Brownb91b8fa2010-01-20 18:18:35 +00001385 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001386 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001387
1388 return 0;
1389}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001390
Milan plzik1b075e32008-01-10 14:39:46 +01001391/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001392static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001393 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001394{
1395 struct snd_soc_dapm_path *path;
1396 int found = 0;
1397
Milan plzik1b075e32008-01-10 14:39:46 +01001398 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001399 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001400 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001401 return -ENODEV;
1402
Richard Purdie2b97eab2006-10-06 18:32:18 +02001403 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001404 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001405 if (path->kcontrol != kcontrol)
1406 continue;
1407
1408 /* found, now check type */
1409 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001410 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001411 break;
1412 }
1413
Mark Brownb91b8fa2010-01-20 18:18:35 +00001414 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001415 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001416
1417 return 0;
1418}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001419
1420/* show dapm widget status in sys fs */
1421static ssize_t dapm_widget_show(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1423{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 struct snd_soc_pcm_runtime *rtd =
1425 container_of(dev, struct snd_soc_pcm_runtime, dev);
1426 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001427 struct snd_soc_dapm_widget *w;
1428 int count = 0;
1429 char *state = "not set";
1430
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001431 list_for_each_entry(w, &codec->card->widgets, list) {
1432 if (w->dapm != &codec->dapm)
1433 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434
1435 /* only display widgets that burnm power */
1436 switch (w->id) {
1437 case snd_soc_dapm_hp:
1438 case snd_soc_dapm_mic:
1439 case snd_soc_dapm_spk:
1440 case snd_soc_dapm_line:
1441 case snd_soc_dapm_micbias:
1442 case snd_soc_dapm_dac:
1443 case snd_soc_dapm_adc:
1444 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001445 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001446 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001447 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001448 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001449 if (w->name)
1450 count += sprintf(buf + count, "%s: %s\n",
1451 w->name, w->power ? "On":"Off");
1452 break;
1453 default:
1454 break;
1455 }
1456 }
1457
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001458 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001459 case SND_SOC_BIAS_ON:
1460 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001461 break;
Mark Brown0be98982008-05-19 12:31:28 +02001462 case SND_SOC_BIAS_PREPARE:
1463 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001464 break;
Mark Brown0be98982008-05-19 12:31:28 +02001465 case SND_SOC_BIAS_STANDBY:
1466 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001467 break;
Mark Brown0be98982008-05-19 12:31:28 +02001468 case SND_SOC_BIAS_OFF:
1469 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470 break;
1471 }
1472 count += sprintf(buf + count, "PM State: %s\n", state);
1473
1474 return count;
1475}
1476
1477static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1478
1479int snd_soc_dapm_sys_add(struct device *dev)
1480{
Troy Kisky12ef1932008-10-13 17:42:14 -07001481 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001482}
1483
1484static void snd_soc_dapm_sys_remove(struct device *dev)
1485{
Mark Brownaef90842009-05-16 17:53:16 +01001486 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001487}
1488
1489/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001490static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001491{
1492 struct snd_soc_dapm_widget *w, *next_w;
1493 struct snd_soc_dapm_path *p, *next_p;
1494
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001495 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1496 if (w->dapm != dapm)
1497 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001498 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001499 /*
1500 * remove source and sink paths associated to this widget.
1501 * While removing the path, remove reference to it from both
1502 * source and sink widgets so that path is removed only once.
1503 */
1504 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1505 list_del(&p->list_sink);
1506 list_del(&p->list_source);
1507 list_del(&p->list);
1508 kfree(p->long_name);
1509 kfree(p);
1510 }
1511 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1512 list_del(&p->list_sink);
1513 list_del(&p->list_source);
1514 list_del(&p->list);
1515 kfree(p->long_name);
1516 kfree(p);
1517 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001518 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001519 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 kfree(w);
1521 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001522}
1523
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001524static struct snd_soc_dapm_widget *dapm_find_widget(
1525 struct snd_soc_dapm_context *dapm, const char *pin,
1526 bool search_other_contexts)
1527{
1528 struct snd_soc_dapm_widget *w;
1529 struct snd_soc_dapm_widget *fallback = NULL;
1530
1531 list_for_each_entry(w, &dapm->card->widgets, list) {
1532 if (!strcmp(w->name, pin)) {
1533 if (w->dapm == dapm)
1534 return w;
1535 else
1536 fallback = w;
1537 }
1538 }
1539
1540 if (search_other_contexts)
1541 return fallback;
1542
1543 return NULL;
1544}
1545
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001546static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001547 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001548{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001550
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001551 if (!w) {
1552 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1553 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001554 }
1555
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001556 w->connected = status;
1557 if (status == 0)
1558 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001559
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001560 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001561}
1562
Richard Purdie2b97eab2006-10-06 18:32:18 +02001563/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001564 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001565 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001566 *
1567 * Walks all dapm audio paths and powers widgets according to their
1568 * stream or path usage.
1569 *
1570 * Returns 0 for success.
1571 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001572int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001574 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575}
Liam Girdwooda5302182008-07-07 13:35:17 +01001576EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001578static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001579 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001580{
1581 struct snd_soc_dapm_path *path;
1582 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001583 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001584 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001585 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001586 const char *source;
1587 char prefixed_sink[80];
1588 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001589 int ret = 0;
1590
Mark Brown88e8b9a2011-03-02 18:18:24 +00001591 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001592 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1593 dapm->codec->name_prefix, route->sink);
1594 sink = prefixed_sink;
1595 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1596 dapm->codec->name_prefix, route->source);
1597 source = prefixed_source;
1598 } else {
1599 sink = route->sink;
1600 source = route->source;
1601 }
1602
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001603 /*
1604 * find src and dest widgets over all widgets but favor a widget from
1605 * current DAPM context
1606 */
1607 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001609 wtsink = w;
1610 if (w->dapm == dapm)
1611 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001612 continue;
1613 }
1614 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001615 wtsource = w;
1616 if (w->dapm == dapm)
1617 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 }
1619 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001620 /* use widget from another DAPM context if not found from this */
1621 if (!wsink)
1622 wsink = wtsink;
1623 if (!wsource)
1624 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001625
1626 if (wsource == NULL || wsink == NULL)
1627 return -ENODEV;
1628
1629 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1630 if (!path)
1631 return -ENOMEM;
1632
1633 path->source = wsource;
1634 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001635 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636 INIT_LIST_HEAD(&path->list);
1637 INIT_LIST_HEAD(&path->list_source);
1638 INIT_LIST_HEAD(&path->list_sink);
1639
1640 /* check for external widgets */
1641 if (wsink->id == snd_soc_dapm_input) {
1642 if (wsource->id == snd_soc_dapm_micbias ||
1643 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001644 wsource->id == snd_soc_dapm_line ||
1645 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646 wsink->ext = 1;
1647 }
1648 if (wsource->id == snd_soc_dapm_output) {
1649 if (wsink->id == snd_soc_dapm_spk ||
1650 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001651 wsink->id == snd_soc_dapm_line ||
1652 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001653 wsource->ext = 1;
1654 }
1655
1656 /* connect static paths */
1657 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001658 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 list_add(&path->list_sink, &wsink->sources);
1660 list_add(&path->list_source, &wsource->sinks);
1661 path->connect = 1;
1662 return 0;
1663 }
1664
1665 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001666 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667 case snd_soc_dapm_adc:
1668 case snd_soc_dapm_dac:
1669 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001670 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 case snd_soc_dapm_input:
1672 case snd_soc_dapm_output:
1673 case snd_soc_dapm_micbias:
1674 case snd_soc_dapm_vmid:
1675 case snd_soc_dapm_pre:
1676 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001677 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001678 case snd_soc_dapm_aif_in:
1679 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001680 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681 list_add(&path->list_sink, &wsink->sources);
1682 list_add(&path->list_source, &wsource->sinks);
1683 path->connect = 1;
1684 return 0;
1685 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001686 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001687 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001688 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001689 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 if (ret != 0)
1691 goto err;
1692 break;
1693 case snd_soc_dapm_switch:
1694 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001695 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001696 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001697 if (ret != 0)
1698 goto err;
1699 break;
1700 case snd_soc_dapm_hp:
1701 case snd_soc_dapm_mic:
1702 case snd_soc_dapm_line:
1703 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001704 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705 list_add(&path->list_sink, &wsink->sources);
1706 list_add(&path->list_source, &wsource->sinks);
1707 path->connect = 0;
1708 return 0;
1709 }
1710 return 0;
1711
1712err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001713 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1714 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 kfree(path);
1716 return ret;
1717}
Mark Brown105f1c22008-05-13 14:52:19 +02001718
1719/**
Mark Brown105f1c22008-05-13 14:52:19 +02001720 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001721 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001722 * @route: audio routes
1723 * @num: number of routes
1724 *
1725 * Connects 2 dapm widgets together via a named audio path. The sink is
1726 * the widget receiving the audio signal, whilst the source is the sender
1727 * of the audio signal.
1728 *
1729 * Returns 0 for success else error. On error all resources can be freed
1730 * with a call to snd_soc_card_free().
1731 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001732int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001733 const struct snd_soc_dapm_route *route, int num)
1734{
1735 int i, ret;
1736
1737 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001738 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001739 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001740 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1741 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001742 return ret;
1743 }
1744 route++;
1745 }
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1750
1751/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001753 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001754 *
1755 * Checks the codec for any new dapm widgets and creates them if found.
1756 *
1757 * Returns 0 for success.
1758 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001759int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760{
1761 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001762 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001764 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001765 {
1766 if (w->new)
1767 continue;
1768
Stephen Warrenfad59882011-04-28 17:37:59 -06001769 if (w->num_kcontrols) {
1770 w->kcontrols = kzalloc(w->num_kcontrols *
1771 sizeof(struct snd_kcontrol *),
1772 GFP_KERNEL);
1773 if (!w->kcontrols)
1774 return -ENOMEM;
1775 }
1776
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 switch(w->id) {
1778 case snd_soc_dapm_switch:
1779 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001780 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001781 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001782 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001783 break;
1784 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001785 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001786 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001787 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001788 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001789 break;
1790 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001791 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001792 w->power_check = dapm_adc_check_power;
1793 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001795 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001796 w->power_check = dapm_dac_check_power;
1797 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001799 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001800 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001801 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 break;
1803 case snd_soc_dapm_input:
1804 case snd_soc_dapm_output:
1805 case snd_soc_dapm_micbias:
1806 case snd_soc_dapm_spk:
1807 case snd_soc_dapm_hp:
1808 case snd_soc_dapm_mic:
1809 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001810 w->power_check = dapm_generic_check_power;
1811 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001812 case snd_soc_dapm_supply:
1813 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814 case snd_soc_dapm_vmid:
1815 case snd_soc_dapm_pre:
1816 case snd_soc_dapm_post:
1817 break;
1818 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001819
1820 /* Read the initial power state from the device */
1821 if (w->reg >= 0) {
1822 val = snd_soc_read(w->codec, w->reg);
1823 val &= 1 << w->shift;
1824 if (w->invert)
1825 val = !val;
1826
1827 if (val)
1828 w->power = 1;
1829 }
1830
Richard Purdie2b97eab2006-10-06 18:32:18 +02001831 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001832
1833 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001834 }
1835
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001836 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1840
1841/**
1842 * snd_soc_dapm_get_volsw - dapm mixer get callback
1843 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001844 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 *
1846 * Callback to get the value of a dapm mixer control.
1847 *
1848 * Returns 0 for success.
1849 */
1850int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_value *ucontrol)
1852{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001853 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1854 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01001855 struct soc_mixer_control *mc =
1856 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001857 unsigned int reg = mc->reg;
1858 unsigned int shift = mc->shift;
1859 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001860 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001861 unsigned int invert = mc->invert;
1862 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863
Richard Purdie2b97eab2006-10-06 18:32:18 +02001864 ucontrol->value.integer.value[0] =
1865 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1866 if (shift != rshift)
1867 ucontrol->value.integer.value[1] =
1868 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1869 if (invert) {
1870 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001871 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001872 if (shift != rshift)
1873 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001874 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875 }
1876
1877 return 0;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1880
1881/**
1882 * snd_soc_dapm_put_volsw - dapm mixer set callback
1883 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001884 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001885 *
1886 * Callback to set the value of a dapm mixer control.
1887 *
1888 * Returns 0 for success.
1889 */
1890int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1892{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001893 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1894 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1895 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001896 struct soc_mixer_control *mc =
1897 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001898 unsigned int reg = mc->reg;
1899 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001900 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001901 unsigned int mask = (1 << fls(max)) - 1;
1902 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001903 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001904 int connect, change;
1905 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06001906 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907
1908 val = (ucontrol->value.integer.value[0] & mask);
1909
1910 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001911 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001912 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914
Stephen Warrenfafd2172011-04-28 17:38:00 -06001915 if (val)
1916 /* new connection */
1917 connect = invert ? 0 : 1;
1918 else
1919 /* old connection must be powered down */
1920 connect = invert ? 1 : 0;
1921
1922 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923
Stephen Warrene9cf7042011-01-27 14:54:05 -07001924 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001925 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06001926 for (wi = 0; wi < wlist->num_widgets; wi++) {
1927 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00001928
Stephen Warrenfafd2172011-04-28 17:38:00 -06001929 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00001930
Stephen Warrenfafd2172011-04-28 17:38:00 -06001931 update.kcontrol = kcontrol;
1932 update.widget = widget;
1933 update.reg = reg;
1934 update.mask = mask;
1935 update.val = val;
1936 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00001937
Stephen Warrenfafd2172011-04-28 17:38:00 -06001938 dapm_mixer_update_power(widget, kcontrol, connect);
1939
1940 widget->dapm->update = NULL;
1941 }
Mark Brown283375c2009-12-07 18:09:03 +00001942 }
1943
Stephen Warrenfafd2172011-04-28 17:38:00 -06001944 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001945 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946}
1947EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1948
1949/**
1950 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1951 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001952 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953 *
1954 * Callback to get the value of a dapm enumerated double mixer control.
1955 *
1956 * Returns 0 for success.
1957 */
1958int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1960{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001961 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1962 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001964 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001966 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001967 ;
1968 val = snd_soc_read(widget->codec, e->reg);
1969 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1970 if (e->shift_l != e->shift_r)
1971 ucontrol->value.enumerated.item[1] =
1972 (val >> e->shift_r) & (bitmask - 1);
1973
1974 return 0;
1975}
1976EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1977
1978/**
1979 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1980 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001981 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 *
1983 * Callback to set the value of a dapm enumerated double mixer control.
1984 *
1985 * Returns 0 for success.
1986 */
1987int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1989{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001990 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1991 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1992 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001994 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001995 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001996 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06001997 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001999 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002001 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002 return -EINVAL;
2003 mux = ucontrol->value.enumerated.item[0];
2004 val = mux << e->shift_l;
2005 mask = (bitmask - 1) << e->shift_l;
2006 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002007 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 return -EINVAL;
2009 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2010 mask |= (bitmask - 1) << e->shift_r;
2011 }
2012
Stephen Warrenfafd2172011-04-28 17:38:00 -06002013 mutex_lock(&codec->mutex);
2014
Mark Brown3a655772009-10-05 17:23:30 +01002015 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002016 if (change) {
2017 for (wi = 0; wi < wlist->num_widgets; wi++) {
2018 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002019
Stephen Warrenfafd2172011-04-28 17:38:00 -06002020 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002021
Stephen Warrenfafd2172011-04-28 17:38:00 -06002022 update.kcontrol = kcontrol;
2023 update.widget = widget;
2024 update.reg = e->reg;
2025 update.mask = mask;
2026 update.val = val;
2027 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002028
Stephen Warrenfafd2172011-04-28 17:38:00 -06002029 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002030
Stephen Warrenfafd2172011-04-28 17:38:00 -06002031 widget->dapm->update = NULL;
2032 }
2033 }
2034
2035 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002036 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037}
2038EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2039
2040/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002041 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2042 * @kcontrol: mixer control
2043 * @ucontrol: control element information
2044 *
2045 * Returns 0 for success.
2046 */
2047int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol)
2049{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002050 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2051 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002052
2053 ucontrol->value.enumerated.item[0] = widget->value;
2054
2055 return 0;
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2058
2059/**
2060 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2061 * @kcontrol: mixer control
2062 * @ucontrol: control element information
2063 *
2064 * Returns 0 for success.
2065 */
2066int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2068{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002069 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2070 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2071 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002072 struct soc_enum *e =
2073 (struct soc_enum *)kcontrol->private_value;
2074 int change;
2075 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002076 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002077
2078 if (ucontrol->value.enumerated.item[0] >= e->max)
2079 return -EINVAL;
2080
Stephen Warrenfafd2172011-04-28 17:38:00 -06002081 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002082
2083 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002084 if (change) {
2085 for (wi = 0; wi < wlist->num_widgets; wi++) {
2086 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002087
Stephen Warrenfafd2172011-04-28 17:38:00 -06002088 widget->value = ucontrol->value.enumerated.item[0];
2089
2090 dapm_mux_update_power(widget, kcontrol, change,
2091 widget->value, e);
2092 }
2093 }
2094
2095 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002096 return ret;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2099
2100/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002101 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2102 * callback
2103 * @kcontrol: mixer control
2104 * @ucontrol: control element information
2105 *
2106 * Callback to get the value of a dapm semi enumerated double mixer control.
2107 *
2108 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2109 * used for handling bitfield coded enumeration for example.
2110 *
2111 * Returns 0 for success.
2112 */
2113int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2115{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002116 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2117 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002118 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002119 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002120
2121 reg_val = snd_soc_read(widget->codec, e->reg);
2122 val = (reg_val >> e->shift_l) & e->mask;
2123 for (mux = 0; mux < e->max; mux++) {
2124 if (val == e->values[mux])
2125 break;
2126 }
2127 ucontrol->value.enumerated.item[0] = mux;
2128 if (e->shift_l != e->shift_r) {
2129 val = (reg_val >> e->shift_r) & e->mask;
2130 for (mux = 0; mux < e->max; mux++) {
2131 if (val == e->values[mux])
2132 break;
2133 }
2134 ucontrol->value.enumerated.item[1] = mux;
2135 }
2136
2137 return 0;
2138}
2139EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2140
2141/**
2142 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2143 * callback
2144 * @kcontrol: mixer control
2145 * @ucontrol: control element information
2146 *
2147 * Callback to set the value of a dapm semi enumerated double mixer control.
2148 *
2149 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2150 * used for handling bitfield coded enumeration for example.
2151 *
2152 * Returns 0 for success.
2153 */
2154int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002157 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2158 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2159 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002160 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002161 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002162 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002163 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002164 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002165
2166 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2167 return -EINVAL;
2168 mux = ucontrol->value.enumerated.item[0];
2169 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2170 mask = e->mask << e->shift_l;
2171 if (e->shift_l != e->shift_r) {
2172 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2173 return -EINVAL;
2174 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2175 mask |= e->mask << e->shift_r;
2176 }
2177
Stephen Warrenfafd2172011-04-28 17:38:00 -06002178 mutex_lock(&codec->mutex);
2179
Mark Brown3a655772009-10-05 17:23:30 +01002180 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002181 if (change) {
2182 for (wi = 0; wi < wlist->num_widgets; wi++) {
2183 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002184
Stephen Warrenfafd2172011-04-28 17:38:00 -06002185 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002186
Stephen Warrenfafd2172011-04-28 17:38:00 -06002187 update.kcontrol = kcontrol;
2188 update.widget = widget;
2189 update.reg = e->reg;
2190 update.mask = mask;
2191 update.val = val;
2192 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002193
Stephen Warrenfafd2172011-04-28 17:38:00 -06002194 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002195
Stephen Warrenfafd2172011-04-28 17:38:00 -06002196 widget->dapm->update = NULL;
2197 }
2198 }
2199
2200 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002201 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002202}
2203EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2204
2205/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002206 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2207 *
2208 * @kcontrol: mixer control
2209 * @uinfo: control element information
2210 *
2211 * Callback to provide information about a pin switch control.
2212 */
2213int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_info *uinfo)
2215{
2216 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2217 uinfo->count = 1;
2218 uinfo->value.integer.min = 0;
2219 uinfo->value.integer.max = 1;
2220
2221 return 0;
2222}
2223EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2224
2225/**
2226 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2227 *
2228 * @kcontrol: mixer control
2229 * @ucontrol: Value
2230 */
2231int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2235 const char *pin = (const char *)kcontrol->private_value;
2236
2237 mutex_lock(&codec->mutex);
2238
2239 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002240 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002241
2242 mutex_unlock(&codec->mutex);
2243
2244 return 0;
2245}
2246EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2247
2248/**
2249 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2250 *
2251 * @kcontrol: mixer control
2252 * @ucontrol: Value
2253 */
2254int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
2256{
2257 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2258 const char *pin = (const char *)kcontrol->private_value;
2259
2260 mutex_lock(&codec->mutex);
2261
2262 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002263 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002264 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002265 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002266
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002267 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002268
2269 mutex_unlock(&codec->mutex);
2270
2271 return 0;
2272}
2273EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2274
2275/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002277 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 * @widget: widget template
2279 *
2280 * Creates a new dapm control based upon the template.
2281 *
2282 * Returns 0 for success else error.
2283 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002284int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 const struct snd_soc_dapm_widget *widget)
2286{
2287 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002288 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289
2290 if ((w = dapm_cnew_widget(widget)) == NULL)
2291 return -ENOMEM;
2292
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002293 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002294 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002295 name_len += 1 + strlen(dapm->codec->name_prefix);
2296 w->name = kmalloc(name_len, GFP_KERNEL);
2297 if (w->name == NULL) {
2298 kfree(w);
2299 return -ENOMEM;
2300 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002301 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002302 snprintf(w->name, name_len, "%s %s",
2303 dapm->codec->name_prefix, widget->name);
2304 else
2305 snprintf(w->name, name_len, "%s", widget->name);
2306
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002307 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002308 w->dapm = dapm;
2309 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002310 INIT_LIST_HEAD(&w->sources);
2311 INIT_LIST_HEAD(&w->sinks);
2312 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002313 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002314
2315 /* machine layer set ups unconnected pins and insertions */
2316 w->connected = 1;
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2320
2321/**
Mark Brown4ba13272008-05-13 14:51:19 +02002322 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002323 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002324 * @widget: widget array
2325 * @num: number of widgets
2326 *
2327 * Creates new DAPM controls based upon the templates.
2328 *
2329 * Returns 0 for success else error.
2330 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002331int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002332 const struct snd_soc_dapm_widget *widget,
2333 int num)
2334{
2335 int i, ret;
2336
2337 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002338 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002339 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002340 dev_err(dapm->dev,
2341 "ASoC: Failed to create DAPM control %s: %d\n",
2342 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002343 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002344 }
Mark Brown4ba13272008-05-13 14:51:19 +02002345 widget++;
2346 }
2347 return 0;
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2350
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002351static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002352 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353{
2354 struct snd_soc_dapm_widget *w;
2355
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002356 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002358 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002360 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2361 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002362 if (strstr(w->sname, stream)) {
2363 switch(event) {
2364 case SND_SOC_DAPM_STREAM_START:
2365 w->active = 1;
2366 break;
2367 case SND_SOC_DAPM_STREAM_STOP:
2368 w->active = 0;
2369 break;
2370 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2374 break;
2375 }
2376 }
2377 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002379 dapm_power_widgets(dapm, event);
2380}
2381
2382/**
2383 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2384 * @rtd: PCM runtime data
2385 * @stream: stream name
2386 * @event: stream event
2387 *
2388 * Sends a stream event to the dapm core. The core then makes any
2389 * necessary widget power changes.
2390 *
2391 * Returns 0 for success else error.
2392 */
2393int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2394 const char *stream, int event)
2395{
2396 struct snd_soc_codec *codec = rtd->codec;
2397
2398 if (stream == NULL)
2399 return 0;
2400
2401 mutex_lock(&codec->mutex);
2402 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002403 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 return 0;
2405}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406
2407/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002408 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002409 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002410 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411 *
Mark Brown74b8f952009-06-06 11:26:15 +01002412 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002413 * a valid audio route and active audio stream.
2414 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2415 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002416 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002417int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002419 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002420}
Liam Girdwooda5302182008-07-07 13:35:17 +01002421EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002422
2423/**
Mark Brownda341832010-03-15 19:23:37 +00002424 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002425 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002426 * @pin: pin name
2427 *
2428 * Enables input/output pin regardless of any other state. This is
2429 * intended for use with microphone bias supplies used in microphone
2430 * jack detection.
2431 *
2432 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2433 * do any widget power switching.
2434 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002435int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2436 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002437{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002438 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002439
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002440 if (!w) {
2441 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2442 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002443 }
2444
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002445 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2446 w->connected = 1;
2447 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002448
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002449 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002450}
2451EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2452
2453/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002454 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002455 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002456 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002457 *
Mark Brown74b8f952009-06-06 11:26:15 +01002458 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002459 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2460 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002461 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002462int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2463 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002464{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002465 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002466}
2467EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2468
2469/**
Mark Brown5817b522008-09-24 11:23:11 +01002470 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002471 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002472 * @pin: pin name
2473 *
2474 * Marks the specified pin as being not connected, disabling it along
2475 * any parent or child widgets. At present this is identical to
2476 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2477 * additional things such as disabling controls which only affect
2478 * paths through the pin.
2479 *
2480 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2481 * do any widget power switching.
2482 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002483int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002484{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002485 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002486}
2487EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2488
2489/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002490 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002491 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002492 * @pin: audio signal pin endpoint (or start point)
2493 *
2494 * Get audio pin status - connected or disconnected.
2495 *
2496 * Returns 1 for connected otherwise 0.
2497 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002498int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2499 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002500{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002501 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002502
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002503 if (w)
2504 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002505
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002506 return 0;
2507}
Liam Girdwooda5302182008-07-07 13:35:17 +01002508EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002509
2510/**
Mark Brown1547aba2010-05-07 21:11:40 +01002511 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002512 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002513 * @pin: audio signal pin endpoint (or start point)
2514 *
2515 * Mark the given endpoint or pin as ignoring suspend. When the
2516 * system is disabled a path between two endpoints flagged as ignoring
2517 * suspend will not be disabled. The path must already be enabled via
2518 * normal means at suspend time, it will not be turned on if it was not
2519 * already enabled.
2520 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002521int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2522 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002523{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002524 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002525
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002526 if (!w) {
2527 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2528 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002529 }
2530
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002531 w->ignore_suspend = 1;
2532
2533 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002534}
2535EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2536
2537/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002539 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002540 *
2541 * Free all dapm widgets and resources.
2542 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002543void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002544{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002545 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002546 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002547 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002548 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002549}
2550EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2551
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002552static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002553{
Mark Brown51737472009-06-22 13:16:51 +01002554 struct snd_soc_dapm_widget *w;
2555 LIST_HEAD(down_list);
2556 int powerdown = 0;
2557
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002558 list_for_each_entry(w, &dapm->card->widgets, list) {
2559 if (w->dapm != dapm)
2560 continue;
Mark Brown51737472009-06-22 13:16:51 +01002561 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002562 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002563 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002564 powerdown = 1;
2565 }
2566 }
2567
2568 /* If there were no widgets to power down we're already in
2569 * standby.
2570 */
2571 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002572 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002573 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002574 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002575 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002576}
Mark Brown51737472009-06-22 13:16:51 +01002577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002578/*
2579 * snd_soc_dapm_shutdown - callback for system shutdown
2580 */
2581void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2582{
2583 struct snd_soc_codec *codec;
2584
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002585 list_for_each_entry(codec, &card->codec_dev_list, list) {
2586 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002587 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002588 }
Mark Brown51737472009-06-22 13:16:51 +01002589}
2590
Richard Purdie2b97eab2006-10-06 18:32:18 +02002591/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002592MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2594MODULE_LICENSE("GPL");