blob: e8997bb978903a46c711b84ada4866a80cc4bf1b [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>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020045#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/soc-dapm.h>
47#include <sound/initval.h>
48
Richard Purdie2b97eab2006-10-06 18:32:18 +020049/* dapm power sequences - make this per codec in the future */
50static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010051 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
53 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010054 [snd_soc_dapm_aif_in] = 3,
55 [snd_soc_dapm_aif_out] = 3,
56 [snd_soc_dapm_mic] = 4,
57 [snd_soc_dapm_mux] = 5,
58 [snd_soc_dapm_value_mux] = 5,
59 [snd_soc_dapm_dac] = 6,
60 [snd_soc_dapm_mixer] = 7,
61 [snd_soc_dapm_mixer_named_ctl] = 7,
62 [snd_soc_dapm_pga] = 8,
63 [snd_soc_dapm_adc] = 9,
64 [snd_soc_dapm_hp] = 10,
65 [snd_soc_dapm_spk] = 10,
66 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020067};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000068
Richard Purdie2b97eab2006-10-06 18:32:18 +020069static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010070 [snd_soc_dapm_pre] = 0,
71 [snd_soc_dapm_adc] = 1,
72 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010073 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pga] = 4,
75 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010076 [snd_soc_dapm_mixer] = 5,
77 [snd_soc_dapm_dac] = 6,
78 [snd_soc_dapm_mic] = 7,
79 [snd_soc_dapm_micbias] = 8,
80 [snd_soc_dapm_mux] = 9,
81 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010082 [snd_soc_dapm_aif_in] = 10,
83 [snd_soc_dapm_aif_out] = 10,
84 [snd_soc_dapm_supply] = 11,
85 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020086};
87
Troy Kisky12ef1932008-10-13 17:42:14 -070088static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +010089{
90 if (pop_time)
91 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
92}
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +010095{
96 va_list args;
97
98 va_start(args, fmt);
99
100 if (pop_time) {
101 vprintk(fmt, args);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100102 }
103
104 va_end(args);
105}
106
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100108static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200109 const struct snd_soc_dapm_widget *_widget)
110{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100111 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112}
113
Mark Brown452c5ea2009-05-17 21:41:23 +0100114/**
115 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000116 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100117 * @level: level to configure
118 *
119 * Configure the bias (power) levels for the SoC audio device.
120 *
121 * Returns 0 for success else error.
122 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000123static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200124 struct snd_soc_dapm_context *dapm,
125 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100126{
Mark Brown452c5ea2009-05-17 21:41:23 +0100127 int ret = 0;
128
Mark Brownf83fba82009-05-18 15:44:43 +0100129 switch (level) {
130 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200131 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100132 break;
133 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200134 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100135 break;
136 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100138 break;
139 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200140 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100141 break;
142 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200143 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100144 return -EINVAL;
145 }
146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100148 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100149 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 if (dapm->codec && dapm->codec->driver->set_bias_level)
151 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100152 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100154 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100155
156 return ret;
157}
158
Richard Purdie2b97eab2006-10-06 18:32:18 +0200159/* set up initial codec paths */
160static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
161 struct snd_soc_dapm_path *p, int i)
162{
163 switch (w->id) {
164 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000165 case snd_soc_dapm_mixer:
166 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200167 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100168 struct soc_mixer_control *mc = (struct soc_mixer_control *)
169 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400170 unsigned int reg = mc->reg;
171 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100172 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400173 unsigned int mask = (1 << fls(max)) - 1;
174 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175
176 val = snd_soc_read(w->codec, reg);
177 val = (val >> shift) & mask;
178
179 if ((invert && !val) || (!invert && val))
180 p->connect = 1;
181 else
182 p->connect = 0;
183 }
184 break;
185 case snd_soc_dapm_mux: {
186 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
187 int val, item, bitmask;
188
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100189 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200190 ;
191 val = snd_soc_read(w->codec, e->reg);
192 item = (val >> e->shift_l) & (bitmask - 1);
193
194 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100195 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196 if (!(strcmp(p->name, e->texts[i])) && item == i)
197 p->connect = 1;
198 }
199 }
200 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200201 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200202 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200203 w->kcontrols[i].private_value;
204 int val, item;
205
206 val = snd_soc_read(w->codec, e->reg);
207 val = (val >> e->shift_l) & e->mask;
208 for (item = 0; item < e->max; item++) {
209 if (val == e->values[item])
210 break;
211 }
212
213 p->connect = 0;
214 for (i = 0; i < e->max; i++) {
215 if (!(strcmp(p->name, e->texts[i])) && item == i)
216 p->connect = 1;
217 }
218 }
219 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200220 /* does not effect routing - always connected */
221 case snd_soc_dapm_pga:
222 case snd_soc_dapm_output:
223 case snd_soc_dapm_adc:
224 case snd_soc_dapm_input:
225 case snd_soc_dapm_dac:
226 case snd_soc_dapm_micbias:
227 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100228 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100229 case snd_soc_dapm_aif_in:
230 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200231 p->connect = 1;
232 break;
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp:
235 case snd_soc_dapm_mic:
236 case snd_soc_dapm_spk:
237 case snd_soc_dapm_line:
238 case snd_soc_dapm_pre:
239 case snd_soc_dapm_post:
240 p->connect = 0;
241 break;
242 }
243}
244
Mark Brown74b8f952009-06-06 11:26:15 +0100245/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200246static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200247 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
248 struct snd_soc_dapm_path *path, const char *control_name,
249 const struct snd_kcontrol_new *kcontrol)
250{
251 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
252 int i;
253
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100254 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200255 if (!(strcmp(control_name, e->texts[i]))) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200256 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 list_add(&path->list_sink, &dest->sources);
258 list_add(&path->list_source, &src->sinks);
259 path->name = (char*)e->texts[i];
260 dapm_set_path_status(dest, path, 0);
261 return 0;
262 }
263 }
264
265 return -ENODEV;
266}
267
Mark Brown74b8f952009-06-06 11:26:15 +0100268/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200269static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200270 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
271 struct snd_soc_dapm_path *path, const char *control_name)
272{
273 int i;
274
275 /* search for mixer kcontrol */
276 for (i = 0; i < dest->num_kcontrols; i++) {
277 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200278 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200279 list_add(&path->list_sink, &dest->sources);
280 list_add(&path->list_source, &src->sinks);
281 path->name = dest->kcontrols[i].name;
282 dapm_set_path_status(dest, path, i);
283 return 0;
284 }
285 }
286 return -ENODEV;
287}
288
289/* update dapm codec register bits */
290static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
291{
292 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300293 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200294 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200295 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200296 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297
298 /* check for valid widgets */
299 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
300 widget->id == snd_soc_dapm_output ||
301 widget->id == snd_soc_dapm_hp ||
302 widget->id == snd_soc_dapm_mic ||
303 widget->id == snd_soc_dapm_line ||
304 widget->id == snd_soc_dapm_spk)
305 return 0;
306
307 power = widget->power;
308 if (widget->invert)
309 power = (power ? 0:1);
310
311 old = snd_soc_read(codec, widget->reg);
312 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
313
314 change = old != new;
315 if (change) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200316 pop_dbg(card->pop_time, "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700317 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200318 card->pop_time);
319 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000320 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200322 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
323 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200324 return change;
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 Brown219b93f2008-10-28 13:02:31 +0000332 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200334 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200335
336 /* add kcontrol */
337 for (i = 0; i < w->num_kcontrols; i++) {
338
339 /* match name */
340 list_for_each_entry(path, &w->sources, list_sink) {
341
342 /* mixer/mux paths name must match control name */
343 if (path->name != (char*)w->kcontrols[i].name)
344 continue;
345
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000346 /* add dapm control with long name.
347 * for dapm_mixer this is the concatenation of the
348 * mixer and kcontrol name.
349 * for dapm_mixer_named_ctl this is simply the
350 * kcontrol name.
351 */
352 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000353 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000354 name_len += 1 + strlen(w->name);
355
Mark Brown219b93f2008-10-28 13:02:31 +0000356 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000357
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 if (path->long_name == NULL)
359 return -ENOMEM;
360
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000361 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000362 default:
363 snprintf(path->long_name, name_len, "%s %s",
364 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000365 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000366 case snd_soc_dapm_mixer_named_ctl:
367 snprintf(path->long_name, name_len, "%s",
368 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000369 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000370 }
371
Mark Brown219b93f2008-10-28 13:02:31 +0000372 path->long_name[name_len - 1] = '\0';
373
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
375 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200376 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200377 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200378 dev_err(dapm->dev,
379 "asoc: failed to add dapm kcontrol %s: %d\n",
380 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200381 kfree(path->long_name);
382 path->long_name = NULL;
383 return ret;
384 }
385 }
386 }
387 return ret;
388}
389
390/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200391static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200392 struct snd_soc_dapm_widget *w)
393{
394 struct snd_soc_dapm_path *path = NULL;
395 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200396 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 int ret = 0;
398
399 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200400 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200401 return -EINVAL;
402 }
403
404 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200405 ret = snd_ctl_add(card, kcontrol);
406
Richard Purdie2b97eab2006-10-06 18:32:18 +0200407 if (ret < 0)
408 goto err;
409
410 list_for_each_entry(path, &w->sources, list_sink)
411 path->kcontrol = kcontrol;
412
413 return ret;
414
415err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200416 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 return ret;
418}
419
420/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200421static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 struct snd_soc_dapm_widget *w)
423{
Mark Browna6c65732010-03-03 17:45:21 +0000424 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200425 dev_err(w->dapm->dev,
426 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427
Mark Browna6c65732010-03-03 17:45:21 +0000428 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429}
430
431/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200432static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433{
434 struct snd_soc_dapm_path *p;
435
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200436 list_for_each_entry(p, &dapm->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 p->walked = 0;
438}
439
Mark Brown9949788b2010-05-07 20:24:05 +0100440/* We implement power down on suspend by checking the power state of
441 * the ALSA card - when we are suspending the ALSA state for the card
442 * is set to D3.
443 */
444static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
445{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200446 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100447
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100449 case SNDRV_CTL_POWER_D3hot:
450 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100451 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200452 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
453 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100454 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100455 default:
456 return 1;
457 }
458}
459
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460/*
461 * Recursively check for a completed path to an active or physically connected
462 * output widget. Returns number of complete paths.
463 */
464static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
465{
466 struct snd_soc_dapm_path *path;
467 int con = 0;
468
Mark Brown246d0a12009-04-22 18:24:55 +0100469 if (widget->id == snd_soc_dapm_supply)
470 return 0;
471
Mark Brown010ff262009-08-17 17:39:22 +0100472 switch (widget->id) {
473 case snd_soc_dapm_adc:
474 case snd_soc_dapm_aif_out:
475 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100476 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100477 default:
478 break;
479 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480
481 if (widget->connected) {
482 /* connected pin ? */
483 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100484 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200485
486 /* connected jack or spk ? */
487 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300488 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown9949788b2010-05-07 20:24:05 +0100489 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490 }
491
492 list_for_each_entry(path, &widget->sinks, list_source) {
493 if (path->walked)
494 continue;
495
496 if (path->sink && path->connect) {
497 path->walked = 1;
498 con += is_connected_output_ep(path->sink);
499 }
500 }
501
502 return con;
503}
504
505/*
506 * Recursively check for a completed path to an active or physically connected
507 * input widget. Returns number of complete paths.
508 */
509static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
510{
511 struct snd_soc_dapm_path *path;
512 int con = 0;
513
Mark Brown246d0a12009-04-22 18:24:55 +0100514 if (widget->id == snd_soc_dapm_supply)
515 return 0;
516
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100518 switch (widget->id) {
519 case snd_soc_dapm_dac:
520 case snd_soc_dapm_aif_in:
521 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100522 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100523 default:
524 break;
525 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526
527 if (widget->connected) {
528 /* connected pin ? */
529 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100530 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531
532 /* connected VMID/Bias for lower pops */
533 if (widget->id == snd_soc_dapm_vmid)
Mark Brown9949788b2010-05-07 20:24:05 +0100534 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535
536 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300537 if (widget->id == snd_soc_dapm_mic ||
538 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown9949788b2010-05-07 20:24:05 +0100539 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540 }
541
542 list_for_each_entry(path, &widget->sources, list_sink) {
543 if (path->walked)
544 continue;
545
546 if (path->source && path->connect) {
547 path->walked = 1;
548 con += is_connected_input_ep(path->source);
549 }
550 }
551
552 return con;
553}
554
555/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300556 * Handler for generic register modifier widget.
557 */
558int dapm_reg_event(struct snd_soc_dapm_widget *w,
559 struct snd_kcontrol *kcontrol, int event)
560{
561 unsigned int val;
562
563 if (SND_SOC_DAPM_EVENT_ON(event))
564 val = w->on_val;
565 else
566 val = w->off_val;
567
568 snd_soc_update_bits(w->codec, -(w->reg + 1),
569 w->mask << w->shift, val << w->shift);
570
571 return 0;
572}
Mark Brown11589412008-07-29 11:42:23 +0100573EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300574
Mark Brown025756ec2009-04-13 11:09:18 +0100575/* Standard power change method, used to apply power changes to most
576 * widgets.
577 */
578static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
579{
580 int ret;
581
582 /* call any power change event handlers */
583 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200584 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756ec2009-04-13 11:09:18 +0100585 w->power ? "on" : "off",
586 w->name, w->event_flags);
587
588 /* power up pre event */
589 if (w->power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
592 if (ret < 0)
593 return ret;
594 }
595
596 /* power down pre event */
597 if (!w->power && w->event &&
598 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
599 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
600 if (ret < 0)
601 return ret;
602 }
603
Mark Brown025756ec2009-04-13 11:09:18 +0100604 dapm_update_bits(w);
605
Mark Brown025756ec2009-04-13 11:09:18 +0100606 /* power up post event */
607 if (w->power && w->event &&
608 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
609 ret = w->event(w,
610 NULL, SND_SOC_DAPM_POST_PMU);
611 if (ret < 0)
612 return ret;
613 }
614
615 /* power down post event */
616 if (!w->power && w->event &&
617 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
618 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
619 if (ret < 0)
620 return ret;
621 }
622
623 return 0;
624}
625
Mark Browncd0f2d42009-04-20 16:56:59 +0100626/* Generic check to see if a widget should be powered.
627 */
628static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
629{
630 int in, out;
631
632 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200633 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100634 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200635 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100636 return out != 0 && in != 0;
637}
638
Mark Brown6ea31b92009-04-20 17:15:41 +0100639/* Check to see if an ADC has power */
640static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
641{
642 int in;
643
644 if (w->active) {
645 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200646 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100647 return in != 0;
648 } else {
649 return dapm_generic_check_power(w);
650 }
651}
652
653/* Check to see if a DAC has power */
654static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
655{
656 int out;
657
658 if (w->active) {
659 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200660 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100661 return out != 0;
662 } else {
663 return dapm_generic_check_power(w);
664 }
665}
666
Mark Brown246d0a12009-04-22 18:24:55 +0100667/* Check to see if a power supply is needed */
668static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
669{
670 struct snd_soc_dapm_path *path;
671 int power = 0;
672
673 /* Check if one of our outputs is connected */
674 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100675 if (path->connected &&
676 !path->connected(path->source, path->sink))
677 continue;
678
Mark Brown246d0a12009-04-22 18:24:55 +0100679 if (path->sink && path->sink->power_check &&
680 path->sink->power_check(path->sink)) {
681 power = 1;
682 break;
683 }
684 }
685
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200686 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100687
688 return power;
689}
690
Mark Brown38357ab2009-06-06 19:03:23 +0100691static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
692 struct snd_soc_dapm_widget *b,
693 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000694{
Mark Brownd207c682009-12-07 17:13:55 +0000695 if (a->codec != b->codec)
696 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100697 if (sort[a->id] != sort[b->id])
698 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100699 if (a->reg != b->reg)
700 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000701
Mark Brown38357ab2009-06-06 19:03:23 +0100702 return 0;
703}
Mark Brown42aa3412009-03-01 19:21:10 +0000704
Mark Brown38357ab2009-06-06 19:03:23 +0100705/* Insert a widget in order into a DAPM power sequence. */
706static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
707 struct list_head *list,
708 int sort[])
709{
710 struct snd_soc_dapm_widget *w;
711
712 list_for_each_entry(w, list, power_list)
713 if (dapm_seq_compare(new_widget, w, sort) < 0) {
714 list_add_tail(&new_widget->power_list, &w->power_list);
715 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000716 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100717
Mark Brown38357ab2009-06-06 19:03:23 +0100718 list_add_tail(&new_widget->power_list, list);
719}
Mark Brown42aa3412009-03-01 19:21:10 +0000720
Mark Brownb22ead22009-06-07 12:51:26 +0100721/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200722static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100723 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100724{
725 struct snd_soc_dapm_widget *w;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200726 struct snd_soc_card *card = dapm->card;
Mark Brown81628102009-06-07 13:21:24 +0100727 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100728 unsigned int value = 0;
729 unsigned int mask = 0;
730 unsigned int cur_mask;
731
732 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
733 power_list)->reg;
734
735 list_for_each_entry(w, pending, power_list) {
736 cur_mask = 1 << w->shift;
737 BUG_ON(reg != w->reg);
738
739 if (w->invert)
740 power = !w->power;
741 else
742 power = w->power;
743
744 mask |= cur_mask;
745 if (power)
746 value |= cur_mask;
747
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200748 pop_dbg(card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100749 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
750 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100751
752 /* power up pre event */
753 if (w->power && w->event &&
754 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200755 pop_dbg(card->pop_time, "pop test : %s PRE_PMU\n",
Mark Brown81628102009-06-07 13:21:24 +0100756 w->name);
757 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
758 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200759 dev_err(dapm->dev,
760 "%s: pre event failed: %d\n",
761 w->name, ret);
Mark Brown81628102009-06-07 13:21:24 +0100762 }
763
764 /* power down pre event */
765 if (!w->power && w->event &&
766 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200767 pop_dbg(card->pop_time, "pop test : %s PRE_PMD\n",
Mark Brown81628102009-06-07 13:21:24 +0100768 w->name);
769 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
770 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200771 dev_err(dapm->dev,
772 "%s: pre event failed: %d\n",
773 w->name, ret);
Mark Brown81628102009-06-07 13:21:24 +0100774 }
Mark Brownb22ead22009-06-07 12:51:26 +0100775 }
776
Mark Brown81628102009-06-07 13:21:24 +0100777 if (reg >= 0) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200778 pop_dbg(card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100779 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200780 value, mask, reg, card->pop_time);
781 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200782 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100783 }
784
785 list_for_each_entry(w, pending, power_list) {
786 /* power up post event */
787 if (w->power && w->event &&
788 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200789 pop_dbg(card->pop_time, "pop test : %s POST_PMU\n",
Mark Brown81628102009-06-07 13:21:24 +0100790 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000791 ret = w->event(w,
792 NULL, SND_SOC_DAPM_POST_PMU);
793 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200794 dev_err(dapm->dev,
795 "%s: post event failed: %d\n",
796 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000797 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100798
Mark Brown81628102009-06-07 13:21:24 +0100799 /* power down post event */
800 if (!w->power && w->event &&
801 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200802 pop_dbg(card->pop_time, "pop test : %s POST_PMD\n",
Mark Brown81628102009-06-07 13:21:24 +0100803 w->name);
804 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
805 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200806 dev_err(dapm->dev,
807 "%s: post event failed: %d\n",
808 w->name, ret);
Mark Brown81628102009-06-07 13:21:24 +0100809 }
Mark Brown42aa3412009-03-01 19:21:10 +0000810 }
Mark Brown42aa3412009-03-01 19:21:10 +0000811}
812
Mark Brownb22ead22009-06-07 12:51:26 +0100813/* Apply a DAPM power sequence.
814 *
815 * We walk over a pre-sorted list of widgets to apply power to. In
816 * order to minimise the number of writes to the device required
817 * multiple widgets will be updated in a single write where possible.
818 * Currently anything that requires more than a single write is not
819 * handled.
820 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200821static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
822 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100823{
824 struct snd_soc_dapm_widget *w, *n;
825 LIST_HEAD(pending);
826 int cur_sort = -1;
827 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100828 int ret;
829
Mark Brownb22ead22009-06-07 12:51:26 +0100830 list_for_each_entry_safe(w, n, list, power_list) {
831 ret = 0;
832
833 /* Do we need to apply any queued changes? */
834 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
835 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200836 dapm_seq_run_coalesced(dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100837
838 INIT_LIST_HEAD(&pending);
839 cur_sort = -1;
840 cur_reg = SND_SOC_NOPM;
841 }
842
Mark Brown163cac02009-06-07 10:12:52 +0100843 switch (w->id) {
844 case snd_soc_dapm_pre:
845 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100846 list_for_each_entry_safe_continue(w, n, list,
847 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100848
Mark Brownb22ead22009-06-07 12:51:26 +0100849 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100850 ret = w->event(w,
851 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100852 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100853 ret = w->event(w,
854 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100855 break;
856
857 case snd_soc_dapm_post:
858 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100859 list_for_each_entry_safe_continue(w, n, list,
860 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100861
Mark Brownb22ead22009-06-07 12:51:26 +0100862 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100863 ret = w->event(w,
864 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100865 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100866 ret = w->event(w,
867 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100868 break;
869
870 case snd_soc_dapm_input:
871 case snd_soc_dapm_output:
872 case snd_soc_dapm_hp:
873 case snd_soc_dapm_mic:
874 case snd_soc_dapm_line:
875 case snd_soc_dapm_spk:
876 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100877 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100878 break;
879
880 default:
Mark Brown81628102009-06-07 13:21:24 +0100881 /* Queue it up for application */
882 cur_sort = sort[w->id];
883 cur_reg = w->reg;
884 list_move(&w->power_list, &pending);
885 break;
Mark Brown163cac02009-06-07 10:12:52 +0100886 }
Mark Brownb22ead22009-06-07 12:51:26 +0100887
888 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200889 dev_err(w->dapm->dev,
890 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100891 }
Mark Brownb22ead22009-06-07 12:51:26 +0100892
893 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200894 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100895}
896
Mark Brown42aa3412009-03-01 19:21:10 +0000897/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200898 * Scan each dapm widget for complete audio path.
899 * A complete path is a route that has valid endpoints i.e.:-
900 *
901 * o DAC to output pin.
902 * o Input Pin to ADC.
903 * o Input pin to Output pin (bypass, sidetone)
904 * o DAC to ADC (loopback).
905 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200906static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200907{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200908 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200909 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100910 LIST_HEAD(up_list);
911 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100912 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100913 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100914 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915
Mark Brown6d3ddc82009-05-16 17:47:29 +0100916 /* Check which widgets we need to power and store them in
917 * lists indicating if they should be powered up or down.
918 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200919 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +0100920 switch (w->id) {
921 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100922 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100923 break;
924 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100925 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100926 break;
927
928 default:
929 if (!w->power_check)
930 continue;
931
Mark Brown9949788b2010-05-07 20:24:05 +0100932 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +0000933 power = w->power_check(w);
Mark Brown9949788b2010-05-07 20:24:05 +0100934 else
935 power = 1;
936 if (power)
937 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100938
Mark Brown6d3ddc82009-05-16 17:47:29 +0100939 if (w->power == power)
940 continue;
941
942 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100943 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100944 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100945 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100946
947 w->power = power;
948 break;
949 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950 }
951
Mark Brownb14b76a52009-08-17 11:55:38 +0100952 /* If there are no DAPM widgets then try to figure out power from the
953 * event type.
954 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200955 if (list_empty(&dapm->widgets)) {
Mark Brownb14b76a52009-08-17 11:55:38 +0100956 switch (event) {
957 case SND_SOC_DAPM_STREAM_START:
958 case SND_SOC_DAPM_STREAM_RESUME:
959 sys_power = 1;
960 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000961 case SND_SOC_DAPM_STREAM_SUSPEND:
962 sys_power = 0;
963 break;
Mark Brownb14b76a52009-08-17 11:55:38 +0100964 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200965 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +0000966 case SND_SOC_BIAS_STANDBY:
967 case SND_SOC_BIAS_OFF:
968 sys_power = 0;
969 break;
970 default:
971 sys_power = 1;
972 break;
973 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000974 break;
Mark Brownb14b76a52009-08-17 11:55:38 +0100975 default:
976 break;
977 }
978 }
979
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200980 if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
981 ret = snd_soc_dapm_set_bias_level(card, dapm,
Mark Browna96ca332010-01-19 22:49:43 +0000982 SND_SOC_BIAS_STANDBY);
983 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200984 dev_err(dapm->dev,
985 "Failed to turn on bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +0000986 }
987
Mark Brown452c5ea2009-05-17 21:41:23 +0100988 /* If we're changing to all on or all off then prepare */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200989 if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
990 (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
991 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
Mark Brown452c5ea2009-05-17 21:41:23 +0100992 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200993 dev_err(dapm->dev,
994 "Failed to prepare bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +0100995 }
996
Mark Brown6d3ddc82009-05-16 17:47:29 +0100997 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200998 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100999
1000 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001001 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001002
Mark Brown452c5ea2009-05-17 21:41:23 +01001003 /* If we just powered the last thing off drop to standby bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001004 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1005 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown452c5ea2009-05-17 21:41:23 +01001006 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001007 dev_err(dapm->dev,
1008 "Failed to apply standby bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001009 }
1010
Mark Browna96ca332010-01-19 22:49:43 +00001011 /* If we're in standby and can support bias off then do that */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001012 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
1013 dapm->idle_bias_off) {
1014 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
Mark Browna96ca332010-01-19 22:49:43 +00001015 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001016 dev_err(dapm->dev,
1017 "Failed to turn off bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +00001018 }
1019
Mark Brown452c5ea2009-05-17 21:41:23 +01001020 /* If we just powered up then move to active bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001021 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1022 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
Mark Brown452c5ea2009-05-17 21:41:23 +01001023 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001024 dev_err(dapm->dev,
1025 "Failed to apply active bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001026 }
1027
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001028 pop_dbg(card->pop_time, "DAPM sequencing finished, waiting %dms\n",
1029 card->pop_time);
1030 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001031
Mark Brown42aa3412009-03-01 19:21:10 +00001032 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033}
1034
Mark Brown79fb9382009-08-21 16:38:13 +01001035#ifdef CONFIG_DEBUG_FS
1036static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1037{
1038 file->private_data = inode->i_private;
1039 return 0;
1040}
1041
1042static ssize_t dapm_widget_power_read_file(struct file *file,
1043 char __user *user_buf,
1044 size_t count, loff_t *ppos)
1045{
1046 struct snd_soc_dapm_widget *w = file->private_data;
1047 char *buf;
1048 int in, out;
1049 ssize_t ret;
1050 struct snd_soc_dapm_path *p = NULL;
1051
1052 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1053 if (!buf)
1054 return -ENOMEM;
1055
1056 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001057 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001058 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001059 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001060
Mark Brownd033c362009-12-04 15:25:56 +00001061 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001062 w->name, w->power ? "On" : "Off", in, out);
1063
Mark Brownd033c362009-12-04 15:25:56 +00001064 if (w->reg >= 0)
1065 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1066 " - R%d(0x%x) bit %d",
1067 w->reg, w->reg, w->shift);
1068
1069 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1070
Mark Brown3eef08b2009-09-14 16:49:00 +01001071 if (w->sname)
1072 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1073 w->sname,
1074 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001075
1076 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001077 if (p->connected && !p->connected(w, p->sink))
1078 continue;
1079
Mark Brown79fb9382009-08-21 16:38:13 +01001080 if (p->connect)
1081 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1082 " in %s %s\n",
1083 p->name ? p->name : "static",
1084 p->source->name);
1085 }
1086 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001087 if (p->connected && !p->connected(w, p->sink))
1088 continue;
1089
Mark Brown79fb9382009-08-21 16:38:13 +01001090 if (p->connect)
1091 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1092 " out %s %s\n",
1093 p->name ? p->name : "static",
1094 p->sink->name);
1095 }
1096
1097 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1098
1099 kfree(buf);
1100 return ret;
1101}
1102
1103static const struct file_operations dapm_widget_power_fops = {
1104 .open = dapm_widget_power_open_file,
1105 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001107};
1108
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001109void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001110{
1111 struct snd_soc_dapm_widget *w;
1112 struct dentry *d;
1113
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001114 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001115 return;
1116
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001117 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown79fb9382009-08-21 16:38:13 +01001118 if (!w->name)
1119 continue;
1120
1121 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001122 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001123 &dapm_widget_power_fops);
1124 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001125 dev_warn(w->dapm->dev,
1126 "ASoC: Failed to create %s debugfs file\n",
1127 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001128 }
1129}
1130#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001131void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001132{
1133}
1134#endif
1135
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001137static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001138 struct snd_kcontrol *kcontrol, int change,
1139 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001140{
1141 struct snd_soc_dapm_path *path;
1142 int found = 0;
1143
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001144 if (widget->id != snd_soc_dapm_mux &&
1145 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001146 return -ENODEV;
1147
Mark Brown3a655772009-10-05 17:23:30 +01001148 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001149 return 0;
1150
1151 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001152 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001153 if (path->kcontrol != kcontrol)
1154 continue;
1155
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001156 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157 continue;
1158
1159 found = 1;
1160 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001161 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162 path->connect = 1; /* new connection */
1163 else
1164 path->connect = 0; /* old connection must be powered down */
1165 }
1166
Mark Brownb91b8fa2010-01-20 18:18:35 +00001167 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001168 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001169
1170 return 0;
1171}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172
Milan plzik1b075e32008-01-10 14:39:46 +01001173/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001174static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001175 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001176{
1177 struct snd_soc_dapm_path *path;
1178 int found = 0;
1179
Milan plzik1b075e32008-01-10 14:39:46 +01001180 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001181 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001182 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 return -ENODEV;
1184
Richard Purdie2b97eab2006-10-06 18:32:18 +02001185 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001186 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187 if (path->kcontrol != kcontrol)
1188 continue;
1189
1190 /* found, now check type */
1191 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001192 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001193 break;
1194 }
1195
Mark Brownb91b8fa2010-01-20 18:18:35 +00001196 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001197 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001198
1199 return 0;
1200}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001201
1202/* show dapm widget status in sys fs */
1203static ssize_t dapm_widget_show(struct device *dev,
1204 struct device_attribute *attr, char *buf)
1205{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 struct snd_soc_pcm_runtime *rtd =
1207 container_of(dev, struct snd_soc_pcm_runtime, dev);
1208 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209 struct snd_soc_dapm_widget *w;
1210 int count = 0;
1211 char *state = "not set";
1212
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001213 list_for_each_entry(w, &codec->dapm.widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214
1215 /* only display widgets that burnm power */
1216 switch (w->id) {
1217 case snd_soc_dapm_hp:
1218 case snd_soc_dapm_mic:
1219 case snd_soc_dapm_spk:
1220 case snd_soc_dapm_line:
1221 case snd_soc_dapm_micbias:
1222 case snd_soc_dapm_dac:
1223 case snd_soc_dapm_adc:
1224 case snd_soc_dapm_pga:
1225 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001226 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001227 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228 if (w->name)
1229 count += sprintf(buf + count, "%s: %s\n",
1230 w->name, w->power ? "On":"Off");
1231 break;
1232 default:
1233 break;
1234 }
1235 }
1236
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001237 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001238 case SND_SOC_BIAS_ON:
1239 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001240 break;
Mark Brown0be98982008-05-19 12:31:28 +02001241 case SND_SOC_BIAS_PREPARE:
1242 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001243 break;
Mark Brown0be98982008-05-19 12:31:28 +02001244 case SND_SOC_BIAS_STANDBY:
1245 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001246 break;
Mark Brown0be98982008-05-19 12:31:28 +02001247 case SND_SOC_BIAS_OFF:
1248 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249 break;
1250 }
1251 count += sprintf(buf + count, "PM State: %s\n", state);
1252
1253 return count;
1254}
1255
1256static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1257
1258int snd_soc_dapm_sys_add(struct device *dev)
1259{
Troy Kisky12ef1932008-10-13 17:42:14 -07001260 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001261}
1262
1263static void snd_soc_dapm_sys_remove(struct device *dev)
1264{
Mark Brownaef90842009-05-16 17:53:16 +01001265 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266}
1267
1268/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001269static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001270{
1271 struct snd_soc_dapm_widget *w, *next_w;
1272 struct snd_soc_dapm_path *p, *next_p;
1273
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001274 list_for_each_entry_safe(w, next_w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275 list_del(&w->list);
1276 kfree(w);
1277 }
1278
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001279 list_for_each_entry_safe(p, next_p, &dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001280 list_del(&p->list);
1281 kfree(p->long_name);
1282 kfree(p);
1283 }
1284}
1285
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001286static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001287 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001288{
1289 struct snd_soc_dapm_widget *w;
1290
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001291 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001292 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001293 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1294 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001295 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001296 /* Allow disabling of forced pins */
1297 if (status == 0)
1298 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001299 return 0;
1300 }
1301 }
1302
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001303 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001304 return -EINVAL;
1305}
1306
Richard Purdie2b97eab2006-10-06 18:32:18 +02001307/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001308 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001309 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001310 *
1311 * Walks all dapm audio paths and powers widgets according to their
1312 * stream or path usage.
1313 *
1314 * Returns 0 for success.
1315 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001316int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001317{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001318 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001319}
Liam Girdwooda5302182008-07-07 13:35:17 +01001320EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001321
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001322static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001323 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001324{
1325 struct snd_soc_dapm_path *path;
1326 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001327 const char *sink = route->sink;
1328 const char *control = route->control;
1329 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001330 int ret = 0;
1331
1332 /* find src and dest widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001333 list_for_each_entry(w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334
1335 if (!wsink && !(strcmp(w->name, sink))) {
1336 wsink = w;
1337 continue;
1338 }
1339 if (!wsource && !(strcmp(w->name, source))) {
1340 wsource = w;
1341 }
1342 }
1343
1344 if (wsource == NULL || wsink == NULL)
1345 return -ENODEV;
1346
1347 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1348 if (!path)
1349 return -ENOMEM;
1350
1351 path->source = wsource;
1352 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001353 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001354 INIT_LIST_HEAD(&path->list);
1355 INIT_LIST_HEAD(&path->list_source);
1356 INIT_LIST_HEAD(&path->list_sink);
1357
1358 /* check for external widgets */
1359 if (wsink->id == snd_soc_dapm_input) {
1360 if (wsource->id == snd_soc_dapm_micbias ||
1361 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001362 wsource->id == snd_soc_dapm_line ||
1363 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001364 wsink->ext = 1;
1365 }
1366 if (wsource->id == snd_soc_dapm_output) {
1367 if (wsink->id == snd_soc_dapm_spk ||
1368 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001369 wsink->id == snd_soc_dapm_line ||
1370 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001371 wsource->ext = 1;
1372 }
1373
1374 /* connect static paths */
1375 if (control == NULL) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001376 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377 list_add(&path->list_sink, &wsink->sources);
1378 list_add(&path->list_source, &wsource->sinks);
1379 path->connect = 1;
1380 return 0;
1381 }
1382
1383 /* connect dynamic paths */
1384 switch(wsink->id) {
1385 case snd_soc_dapm_adc:
1386 case snd_soc_dapm_dac:
1387 case snd_soc_dapm_pga:
1388 case snd_soc_dapm_input:
1389 case snd_soc_dapm_output:
1390 case snd_soc_dapm_micbias:
1391 case snd_soc_dapm_vmid:
1392 case snd_soc_dapm_pre:
1393 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001394 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001395 case snd_soc_dapm_aif_in:
1396 case snd_soc_dapm_aif_out:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001397 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001398 list_add(&path->list_sink, &wsink->sources);
1399 list_add(&path->list_source, &wsource->sinks);
1400 path->connect = 1;
1401 return 0;
1402 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001403 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001404 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001405 &wsink->kcontrols[0]);
1406 if (ret != 0)
1407 goto err;
1408 break;
1409 case snd_soc_dapm_switch:
1410 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001411 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001412 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413 if (ret != 0)
1414 goto err;
1415 break;
1416 case snd_soc_dapm_hp:
1417 case snd_soc_dapm_mic:
1418 case snd_soc_dapm_line:
1419 case snd_soc_dapm_spk:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001420 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001421 list_add(&path->list_sink, &wsink->sources);
1422 list_add(&path->list_source, &wsource->sinks);
1423 path->connect = 0;
1424 return 0;
1425 }
1426 return 0;
1427
1428err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001429 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1430 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001431 kfree(path);
1432 return ret;
1433}
Mark Brown105f1c22008-05-13 14:52:19 +02001434
1435/**
Mark Brown105f1c22008-05-13 14:52:19 +02001436 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001437 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001438 * @route: audio routes
1439 * @num: number of routes
1440 *
1441 * Connects 2 dapm widgets together via a named audio path. The sink is
1442 * the widget receiving the audio signal, whilst the source is the sender
1443 * of the audio signal.
1444 *
1445 * Returns 0 for success else error. On error all resources can be freed
1446 * with a call to snd_soc_card_free().
1447 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001448int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001449 const struct snd_soc_dapm_route *route, int num)
1450{
1451 int i, ret;
1452
1453 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001454 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001455 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001456 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1457 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001458 return ret;
1459 }
1460 route++;
1461 }
1462
1463 return 0;
1464}
1465EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1466
1467/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001469 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470 *
1471 * Checks the codec for any new dapm widgets and creates them if found.
1472 *
1473 * Returns 0 for success.
1474 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001475int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001476{
1477 struct snd_soc_dapm_widget *w;
1478
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001479 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001480 {
1481 if (w->new)
1482 continue;
1483
1484 switch(w->id) {
1485 case snd_soc_dapm_switch:
1486 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001487 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001488 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001489 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001490 break;
1491 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001492 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001493 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001494 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001495 break;
1496 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001497 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001498 w->power_check = dapm_adc_check_power;
1499 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001500 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001501 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001502 w->power_check = dapm_dac_check_power;
1503 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001504 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001505 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001506 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001507 break;
1508 case snd_soc_dapm_input:
1509 case snd_soc_dapm_output:
1510 case snd_soc_dapm_micbias:
1511 case snd_soc_dapm_spk:
1512 case snd_soc_dapm_hp:
1513 case snd_soc_dapm_mic:
1514 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001515 w->power_check = dapm_generic_check_power;
1516 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001517 case snd_soc_dapm_supply:
1518 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 case snd_soc_dapm_vmid:
1520 case snd_soc_dapm_pre:
1521 case snd_soc_dapm_post:
1522 break;
1523 }
1524 w->new = 1;
1525 }
1526
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001527 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001528 return 0;
1529}
1530EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1531
1532/**
1533 * snd_soc_dapm_get_volsw - dapm mixer get callback
1534 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001535 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536 *
1537 * Callback to get the value of a dapm mixer control.
1538 *
1539 * Returns 0 for success.
1540 */
1541int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
1543{
1544 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001545 struct soc_mixer_control *mc =
1546 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001547 unsigned int reg = mc->reg;
1548 unsigned int shift = mc->shift;
1549 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001550 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001551 unsigned int invert = mc->invert;
1552 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001553
Richard Purdie2b97eab2006-10-06 18:32:18 +02001554 ucontrol->value.integer.value[0] =
1555 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1556 if (shift != rshift)
1557 ucontrol->value.integer.value[1] =
1558 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1559 if (invert) {
1560 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001561 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 if (shift != rshift)
1563 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001564 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 }
1566
1567 return 0;
1568}
1569EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1570
1571/**
1572 * snd_soc_dapm_put_volsw - dapm mixer set callback
1573 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001574 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 *
1576 * Callback to set the value of a dapm mixer control.
1577 *
1578 * Returns 0 for success.
1579 */
1580int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001584 struct soc_mixer_control *mc =
1585 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001586 unsigned int reg = mc->reg;
1587 unsigned int shift = mc->shift;
1588 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001589 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001590 unsigned int mask = (1 << fls(max)) - 1;
1591 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001592 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001593 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001594 int ret;
1595
1596 val = (ucontrol->value.integer.value[0] & mask);
1597
1598 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001599 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001600 val_mask = mask << shift;
1601 val = val << shift;
1602 if (shift != rshift) {
1603 val2 = (ucontrol->value.integer.value[1] & mask);
1604 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001605 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001606 val_mask |= mask << rshift;
1607 val |= val2 << rshift;
1608 }
1609
1610 mutex_lock(&widget->codec->mutex);
1611 widget->value = val;
1612
Mark Brown283375c2009-12-07 18:09:03 +00001613 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1614 if (val)
1615 /* new connection */
1616 connect = invert ? 0:1;
1617 else
1618 /* old connection must be powered down */
1619 connect = invert ? 1:0;
1620
1621 dapm_mixer_update_power(widget, kcontrol, connect);
1622 }
1623
Richard Purdie2b97eab2006-10-06 18:32:18 +02001624 if (widget->event) {
1625 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001626 ret = widget->event(widget, kcontrol,
1627 SND_SOC_DAPM_PRE_REG);
1628 if (ret < 0) {
1629 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001630 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001631 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001632 }
1633 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1634 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001635 ret = widget->event(widget, kcontrol,
1636 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001637 } else
1638 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1639
1640out:
1641 mutex_unlock(&widget->codec->mutex);
1642 return ret;
1643}
1644EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1645
1646/**
1647 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1648 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001649 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 *
1651 * Callback to get the value of a dapm enumerated double mixer control.
1652 *
1653 * Returns 0 for success.
1654 */
1655int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
1657{
1658 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1659 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001660 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001662 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001663 ;
1664 val = snd_soc_read(widget->codec, e->reg);
1665 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1666 if (e->shift_l != e->shift_r)
1667 ucontrol->value.enumerated.item[1] =
1668 (val >> e->shift_r) & (bitmask - 1);
1669
1670 return 0;
1671}
1672EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1673
1674/**
1675 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1676 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001677 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001678 *
1679 * Callback to set the value of a dapm enumerated double mixer control.
1680 *
1681 * Returns 0 for success.
1682 */
1683int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1684 struct snd_ctl_elem_value *ucontrol)
1685{
1686 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1687 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001688 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001689 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 int ret = 0;
1691
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001692 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001693 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001694 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695 return -EINVAL;
1696 mux = ucontrol->value.enumerated.item[0];
1697 val = mux << e->shift_l;
1698 mask = (bitmask - 1) << e->shift_l;
1699 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001700 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001701 return -EINVAL;
1702 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1703 mask |= (bitmask - 1) << e->shift_r;
1704 }
1705
1706 mutex_lock(&widget->codec->mutex);
1707 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001708 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1709 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001710
1711 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1712 ret = widget->event(widget,
1713 kcontrol, SND_SOC_DAPM_PRE_REG);
1714 if (ret < 0)
1715 goto out;
1716 }
1717
1718 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1719
1720 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1721 ret = widget->event(widget,
1722 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723
1724out:
1725 mutex_unlock(&widget->codec->mutex);
1726 return ret;
1727}
1728EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1729
1730/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001731 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1732 * @kcontrol: mixer control
1733 * @ucontrol: control element information
1734 *
1735 * Returns 0 for success.
1736 */
1737int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1739{
1740 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1741
1742 ucontrol->value.enumerated.item[0] = widget->value;
1743
1744 return 0;
1745}
1746EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1747
1748/**
1749 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1750 * @kcontrol: mixer control
1751 * @ucontrol: control element information
1752 *
1753 * Returns 0 for success.
1754 */
1755int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
1757{
1758 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1759 struct soc_enum *e =
1760 (struct soc_enum *)kcontrol->private_value;
1761 int change;
1762 int ret = 0;
1763
1764 if (ucontrol->value.enumerated.item[0] >= e->max)
1765 return -EINVAL;
1766
1767 mutex_lock(&widget->codec->mutex);
1768
1769 change = widget->value != ucontrol->value.enumerated.item[0];
1770 widget->value = ucontrol->value.enumerated.item[0];
1771 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1772
1773 mutex_unlock(&widget->codec->mutex);
1774 return ret;
1775}
1776EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1777
1778/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001779 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1780 * callback
1781 * @kcontrol: mixer control
1782 * @ucontrol: control element information
1783 *
1784 * Callback to get the value of a dapm semi enumerated double mixer control.
1785 *
1786 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1787 * used for handling bitfield coded enumeration for example.
1788 *
1789 * Returns 0 for success.
1790 */
1791int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1793{
1794 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001795 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001796 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001797
1798 reg_val = snd_soc_read(widget->codec, e->reg);
1799 val = (reg_val >> e->shift_l) & e->mask;
1800 for (mux = 0; mux < e->max; mux++) {
1801 if (val == e->values[mux])
1802 break;
1803 }
1804 ucontrol->value.enumerated.item[0] = mux;
1805 if (e->shift_l != e->shift_r) {
1806 val = (reg_val >> e->shift_r) & e->mask;
1807 for (mux = 0; mux < e->max; mux++) {
1808 if (val == e->values[mux])
1809 break;
1810 }
1811 ucontrol->value.enumerated.item[1] = mux;
1812 }
1813
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1817
1818/**
1819 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1820 * callback
1821 * @kcontrol: mixer control
1822 * @ucontrol: control element information
1823 *
1824 * Callback to set the value of a dapm semi enumerated double mixer control.
1825 *
1826 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1827 * used for handling bitfield coded enumeration for example.
1828 *
1829 * Returns 0 for success.
1830 */
1831int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1832 struct snd_ctl_elem_value *ucontrol)
1833{
1834 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001835 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001836 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001837 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001838 int ret = 0;
1839
1840 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1841 return -EINVAL;
1842 mux = ucontrol->value.enumerated.item[0];
1843 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1844 mask = e->mask << e->shift_l;
1845 if (e->shift_l != e->shift_r) {
1846 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1847 return -EINVAL;
1848 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1849 mask |= e->mask << e->shift_r;
1850 }
1851
1852 mutex_lock(&widget->codec->mutex);
1853 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001854 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1855 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001856
1857 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1858 ret = widget->event(widget,
1859 kcontrol, SND_SOC_DAPM_PRE_REG);
1860 if (ret < 0)
1861 goto out;
1862 }
1863
1864 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1865
1866 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1867 ret = widget->event(widget,
1868 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001869
1870out:
1871 mutex_unlock(&widget->codec->mutex);
1872 return ret;
1873}
1874EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1875
1876/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001877 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1878 *
1879 * @kcontrol: mixer control
1880 * @uinfo: control element information
1881 *
1882 * Callback to provide information about a pin switch control.
1883 */
1884int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1885 struct snd_ctl_elem_info *uinfo)
1886{
1887 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1888 uinfo->count = 1;
1889 uinfo->value.integer.min = 0;
1890 uinfo->value.integer.max = 1;
1891
1892 return 0;
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1895
1896/**
1897 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1898 *
1899 * @kcontrol: mixer control
1900 * @ucontrol: Value
1901 */
1902int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
1905 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1906 const char *pin = (const char *)kcontrol->private_value;
1907
1908 mutex_lock(&codec->mutex);
1909
1910 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001911 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001912
1913 mutex_unlock(&codec->mutex);
1914
1915 return 0;
1916}
1917EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1918
1919/**
1920 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1921 *
1922 * @kcontrol: mixer control
1923 * @ucontrol: Value
1924 */
1925int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1926 struct snd_ctl_elem_value *ucontrol)
1927{
1928 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1929 const char *pin = (const char *)kcontrol->private_value;
1930
1931 mutex_lock(&codec->mutex);
1932
1933 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001934 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001935 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001936 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001937
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001938 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001939
1940 mutex_unlock(&codec->mutex);
1941
1942 return 0;
1943}
1944EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1945
1946/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001948 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949 * @widget: widget template
1950 *
1951 * Creates a new dapm control based upon the template.
1952 *
1953 * Returns 0 for success else error.
1954 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001955int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956 const struct snd_soc_dapm_widget *widget)
1957{
1958 struct snd_soc_dapm_widget *w;
1959
1960 if ((w = dapm_cnew_widget(widget)) == NULL)
1961 return -ENOMEM;
1962
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001963 w->dapm = dapm;
1964 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965 INIT_LIST_HEAD(&w->sources);
1966 INIT_LIST_HEAD(&w->sinks);
1967 INIT_LIST_HEAD(&w->list);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001968 list_add(&w->list, &dapm->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001969
1970 /* machine layer set ups unconnected pins and insertions */
1971 w->connected = 1;
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1975
1976/**
Mark Brown4ba13272008-05-13 14:51:19 +02001977 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001978 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02001979 * @widget: widget array
1980 * @num: number of widgets
1981 *
1982 * Creates new DAPM controls based upon the templates.
1983 *
1984 * Returns 0 for success else error.
1985 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001986int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02001987 const struct snd_soc_dapm_widget *widget,
1988 int num)
1989{
1990 int i, ret;
1991
1992 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001993 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001994 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001995 dev_err(dapm->dev,
1996 "ASoC: Failed to create DAPM control %s: %d\n",
1997 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001998 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001999 }
Mark Brown4ba13272008-05-13 14:51:19 +02002000 widget++;
2001 }
2002 return 0;
2003}
2004EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2005
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002006static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008{
2009 struct snd_soc_dapm_widget *w;
2010
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002011 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 {
2013 if (!w->sname)
2014 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002015 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2016 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 if (strstr(w->sname, stream)) {
2018 switch(event) {
2019 case SND_SOC_DAPM_STREAM_START:
2020 w->active = 1;
2021 break;
2022 case SND_SOC_DAPM_STREAM_STOP:
2023 w->active = 0;
2024 break;
2025 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2029 break;
2030 }
2031 }
2032 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002034 dapm_power_widgets(dapm, event);
2035}
2036
2037/**
2038 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2039 * @rtd: PCM runtime data
2040 * @stream: stream name
2041 * @event: stream event
2042 *
2043 * Sends a stream event to the dapm core. The core then makes any
2044 * necessary widget power changes.
2045 *
2046 * Returns 0 for success else error.
2047 */
2048int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2049 const char *stream, int event)
2050{
2051 struct snd_soc_codec *codec = rtd->codec;
2052
2053 if (stream == NULL)
2054 return 0;
2055
2056 mutex_lock(&codec->mutex);
2057 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002058 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2062
2063/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002064 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002065 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002066 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 *
Mark Brown74b8f952009-06-06 11:26:15 +01002068 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002069 * a valid audio route and active audio stream.
2070 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2071 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002073int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002075 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076}
Liam Girdwooda5302182008-07-07 13:35:17 +01002077EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078
2079/**
Mark Brownda341832010-03-15 19:23:37 +00002080 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002081 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002082 * @pin: pin name
2083 *
2084 * Enables input/output pin regardless of any other state. This is
2085 * intended for use with microphone bias supplies used in microphone
2086 * jack detection.
2087 *
2088 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2089 * do any widget power switching.
2090 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002091int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2092 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002093{
2094 struct snd_soc_dapm_widget *w;
2095
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002096 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brownda341832010-03-15 19:23:37 +00002097 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002098 dev_dbg(w->dapm->dev,
2099 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002100 w->connected = 1;
2101 w->force = 1;
2102 return 0;
2103 }
2104 }
2105
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002106 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002107 return -EINVAL;
2108}
2109EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2110
2111/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002112 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002113 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002114 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002115 *
Mark Brown74b8f952009-06-06 11:26:15 +01002116 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002117 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2118 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002119 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002120int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2121 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002122{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002124}
2125EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2126
2127/**
Mark Brown5817b522008-09-24 11:23:11 +01002128 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002129 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002130 * @pin: pin name
2131 *
2132 * Marks the specified pin as being not connected, disabling it along
2133 * any parent or child widgets. At present this is identical to
2134 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2135 * additional things such as disabling controls which only affect
2136 * paths through the pin.
2137 *
2138 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2139 * do any widget power switching.
2140 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002141int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002142{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002143 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002144}
2145EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2146
2147/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002148 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002149 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002150 * @pin: audio signal pin endpoint (or start point)
2151 *
2152 * Get audio pin status - connected or disconnected.
2153 *
2154 * Returns 1 for connected otherwise 0.
2155 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002156int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2157 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002158{
2159 struct snd_soc_dapm_widget *w;
2160
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002161 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002162 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002163 return w->connected;
2164 }
2165
2166 return 0;
2167}
Liam Girdwooda5302182008-07-07 13:35:17 +01002168EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002169
2170/**
Mark Brown1547aba2010-05-07 21:11:40 +01002171 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002172 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002173 * @pin: audio signal pin endpoint (or start point)
2174 *
2175 * Mark the given endpoint or pin as ignoring suspend. When the
2176 * system is disabled a path between two endpoints flagged as ignoring
2177 * suspend will not be disabled. The path must already be enabled via
2178 * normal means at suspend time, it will not be turned on if it was not
2179 * already enabled.
2180 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002181int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2182 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002183{
2184 struct snd_soc_dapm_widget *w;
2185
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002186 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown1547aba2010-05-07 21:11:40 +01002187 if (!strcmp(w->name, pin)) {
2188 w->ignore_suspend = 1;
2189 return 0;
2190 }
2191 }
2192
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002193 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002194 return -EINVAL;
2195}
2196EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2197
2198/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002200 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 *
2202 * Free all dapm widgets and resources.
2203 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002204void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002206 snd_soc_dapm_sys_remove(dapm->dev);
2207 dapm_free_widgets(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208}
2209EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2210
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002211static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002212{
Mark Brown51737472009-06-22 13:16:51 +01002213 struct snd_soc_dapm_widget *w;
2214 LIST_HEAD(down_list);
2215 int powerdown = 0;
2216
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002217 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown51737472009-06-22 13:16:51 +01002218 if (w->power) {
2219 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002220 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002221 powerdown = 1;
2222 }
2223 }
2224
2225 /* If there were no widgets to power down we're already in
2226 * standby.
2227 */
2228 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002229 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2230 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2231 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002232 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002233}
Mark Brown51737472009-06-22 13:16:51 +01002234
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002235/*
2236 * snd_soc_dapm_shutdown - callback for system shutdown
2237 */
2238void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2239{
2240 struct snd_soc_codec *codec;
2241
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002242 list_for_each_entry(codec, &card->codec_dev_list, list) {
2243 soc_dapm_shutdown_codec(&codec->dapm);
2244 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2245 }
Mark Brown51737472009-06-22 13:16:51 +01002246}
2247
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002249MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2251MODULE_LICENSE("GPL");