blob: 5a5db3f58f0f80cb90936c4302e6d4b891e70753 [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 Brown15e4c722008-07-02 11:51:20 +010089{
90 if (pop_time)
91 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
92}
93
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020094static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020097 char *buf;
98
99 if (!pop_time)
100 return;
101
102 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
103 if (buf == NULL)
104 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100105
106 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200107 vsnprintf(buf, PAGE_SIZE, fmt, args);
108 dev_info(dev, buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100109 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200110
111 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100112}
113
Richard Purdie2b97eab2006-10-06 18:32:18 +0200114/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100115static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200116 const struct snd_soc_dapm_widget *_widget)
117{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100118 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200119}
120
Mark Brown452c5ea2009-05-17 21:41:23 +0100121/**
122 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000123 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100124 * @level: level to configure
125 *
126 * Configure the bias (power) levels for the SoC audio device.
127 *
128 * Returns 0 for success else error.
129 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000130static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200131 struct snd_soc_dapm_context *dapm,
132 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100133{
Mark Brown452c5ea2009-05-17 21:41:23 +0100134 int ret = 0;
135
Mark Brownf83fba82009-05-18 15:44:43 +0100136 switch (level) {
137 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200138 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100139 break;
140 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200141 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100142 break;
143 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100151 return -EINVAL;
152 }
153
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000154 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100155 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100156 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200157 if (dapm->codec && dapm->codec->driver->set_bias_level)
158 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100159 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200160 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100161 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100162
163 return ret;
164}
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* set up initial codec paths */
167static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
168 struct snd_soc_dapm_path *p, int i)
169{
170 switch (w->id) {
171 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000172 case snd_soc_dapm_mixer:
173 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200174 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100175 struct soc_mixer_control *mc = (struct soc_mixer_control *)
176 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400177 unsigned int reg = mc->reg;
178 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100179 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400180 unsigned int mask = (1 << fls(max)) - 1;
181 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200182
183 val = snd_soc_read(w->codec, reg);
184 val = (val >> shift) & mask;
185
186 if ((invert && !val) || (!invert && val))
187 p->connect = 1;
188 else
189 p->connect = 0;
190 }
191 break;
192 case snd_soc_dapm_mux: {
193 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
194 int val, item, bitmask;
195
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100196 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200197 ;
198 val = snd_soc_read(w->codec, e->reg);
199 item = (val >> e->shift_l) & (bitmask - 1);
200
201 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100202 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200203 if (!(strcmp(p->name, e->texts[i])) && item == i)
204 p->connect = 1;
205 }
206 }
207 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200208 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200209 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200210 w->kcontrols[i].private_value;
211 int val, item;
212
213 val = snd_soc_read(w->codec, e->reg);
214 val = (val >> e->shift_l) & e->mask;
215 for (item = 0; item < e->max; item++) {
216 if (val == e->values[item])
217 break;
218 }
219
220 p->connect = 0;
221 for (i = 0; i < e->max; i++) {
222 if (!(strcmp(p->name, e->texts[i])) && item == i)
223 p->connect = 1;
224 }
225 }
226 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200227 /* does not effect routing - always connected */
228 case snd_soc_dapm_pga:
229 case snd_soc_dapm_output:
230 case snd_soc_dapm_adc:
231 case snd_soc_dapm_input:
232 case snd_soc_dapm_dac:
233 case snd_soc_dapm_micbias:
234 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100235 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100236 case snd_soc_dapm_aif_in:
237 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200238 p->connect = 1;
239 break;
240 /* does effect routing - dynamically connected */
241 case snd_soc_dapm_hp:
242 case snd_soc_dapm_mic:
243 case snd_soc_dapm_spk:
244 case snd_soc_dapm_line:
245 case snd_soc_dapm_pre:
246 case snd_soc_dapm_post:
247 p->connect = 0;
248 break;
249 }
250}
251
Mark Brown74b8f952009-06-06 11:26:15 +0100252/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200253static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200254 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
255 struct snd_soc_dapm_path *path, const char *control_name,
256 const struct snd_kcontrol_new *kcontrol)
257{
258 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
259 int i;
260
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100261 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200262 if (!(strcmp(control_name, e->texts[i]))) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200263 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200264 list_add(&path->list_sink, &dest->sources);
265 list_add(&path->list_source, &src->sinks);
266 path->name = (char*)e->texts[i];
267 dapm_set_path_status(dest, path, 0);
268 return 0;
269 }
270 }
271
272 return -ENODEV;
273}
274
Mark Brown74b8f952009-06-06 11:26:15 +0100275/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200276static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200277 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
278 struct snd_soc_dapm_path *path, const char *control_name)
279{
280 int i;
281
282 /* search for mixer kcontrol */
283 for (i = 0; i < dest->num_kcontrols; i++) {
284 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200285 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200286 list_add(&path->list_sink, &dest->sources);
287 list_add(&path->list_source, &src->sinks);
288 path->name = dest->kcontrols[i].name;
289 dapm_set_path_status(dest, path, i);
290 return 0;
291 }
292 }
293 return -ENODEV;
294}
295
296/* update dapm codec register bits */
297static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
298{
299 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300300 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200302 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200303 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200304
305 /* check for valid widgets */
306 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
307 widget->id == snd_soc_dapm_output ||
308 widget->id == snd_soc_dapm_hp ||
309 widget->id == snd_soc_dapm_mic ||
310 widget->id == snd_soc_dapm_line ||
311 widget->id == snd_soc_dapm_spk)
312 return 0;
313
314 power = widget->power;
315 if (widget->invert)
316 power = (power ? 0:1);
317
318 old = snd_soc_read(codec, widget->reg);
319 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
320
321 change = old != new;
322 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200323 pop_dbg(dapm->dev, card->pop_time,
324 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700325 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200326 card->pop_time);
327 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000328 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200330 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
331 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200332 return change;
333}
334
Richard Purdie2b97eab2006-10-06 18:32:18 +0200335/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200336static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200337 struct snd_soc_dapm_widget *w)
338{
339 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000340 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200342 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343
344 /* add kcontrol */
345 for (i = 0; i < w->num_kcontrols; i++) {
346
347 /* match name */
348 list_for_each_entry(path, &w->sources, list_sink) {
349
350 /* mixer/mux paths name must match control name */
351 if (path->name != (char*)w->kcontrols[i].name)
352 continue;
353
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000354 /* add dapm control with long name.
355 * for dapm_mixer this is the concatenation of the
356 * mixer and kcontrol name.
357 * for dapm_mixer_named_ctl this is simply the
358 * kcontrol name.
359 */
360 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000361 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000362 name_len += 1 + strlen(w->name);
363
Mark Brown219b93f2008-10-28 13:02:31 +0000364 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000365
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 if (path->long_name == NULL)
367 return -ENOMEM;
368
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000369 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000370 default:
371 snprintf(path->long_name, name_len, "%s %s",
372 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000373 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000374 case snd_soc_dapm_mixer_named_ctl:
375 snprintf(path->long_name, name_len, "%s",
376 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000377 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000378 }
379
Mark Brown219b93f2008-10-28 13:02:31 +0000380 path->long_name[name_len - 1] = '\0';
381
Richard Purdie2b97eab2006-10-06 18:32:18 +0200382 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
383 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200384 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200385 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200386 dev_err(dapm->dev,
387 "asoc: failed to add dapm kcontrol %s: %d\n",
388 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200389 kfree(path->long_name);
390 path->long_name = NULL;
391 return ret;
392 }
393 }
394 }
395 return ret;
396}
397
398/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200399static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200400 struct snd_soc_dapm_widget *w)
401{
402 struct snd_soc_dapm_path *path = NULL;
403 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200404 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200405 int ret = 0;
406
407 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200408 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200409 return -EINVAL;
410 }
411
412 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200413 ret = snd_ctl_add(card, kcontrol);
414
Richard Purdie2b97eab2006-10-06 18:32:18 +0200415 if (ret < 0)
416 goto err;
417
418 list_for_each_entry(path, &w->sources, list_sink)
419 path->kcontrol = kcontrol;
420
421 return ret;
422
423err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200424 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200425 return ret;
426}
427
428/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200429static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200430 struct snd_soc_dapm_widget *w)
431{
Mark Browna6c65732010-03-03 17:45:21 +0000432 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200433 dev_err(w->dapm->dev,
434 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200435
Mark Browna6c65732010-03-03 17:45:21 +0000436 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437}
438
439/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200440static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200441{
442 struct snd_soc_dapm_path *p;
443
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200444 list_for_each_entry(p, &dapm->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200445 p->walked = 0;
446}
447
Mark Brown99497882010-05-07 20:24:05 +0100448/* We implement power down on suspend by checking the power state of
449 * the ALSA card - when we are suspending the ALSA state for the card
450 * is set to D3.
451 */
452static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
453{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200454 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100455
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100457 case SNDRV_CTL_POWER_D3hot:
458 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100459 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200460 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
461 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100462 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100463 default:
464 return 1;
465 }
466}
467
Richard Purdie2b97eab2006-10-06 18:32:18 +0200468/*
469 * Recursively check for a completed path to an active or physically connected
470 * output widget. Returns number of complete paths.
471 */
472static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
473{
474 struct snd_soc_dapm_path *path;
475 int con = 0;
476
Mark Brown246d0a12009-04-22 18:24:55 +0100477 if (widget->id == snd_soc_dapm_supply)
478 return 0;
479
Mark Brown010ff262009-08-17 17:39:22 +0100480 switch (widget->id) {
481 case snd_soc_dapm_adc:
482 case snd_soc_dapm_aif_out:
483 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100484 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100485 default:
486 break;
487 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200488
489 if (widget->connected) {
490 /* connected pin ? */
491 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100492 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200493
494 /* connected jack or spk ? */
495 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300496 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100497 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200498 }
499
500 list_for_each_entry(path, &widget->sinks, list_source) {
501 if (path->walked)
502 continue;
503
504 if (path->sink && path->connect) {
505 path->walked = 1;
506 con += is_connected_output_ep(path->sink);
507 }
508 }
509
510 return con;
511}
512
513/*
514 * Recursively check for a completed path to an active or physically connected
515 * input widget. Returns number of complete paths.
516 */
517static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
518{
519 struct snd_soc_dapm_path *path;
520 int con = 0;
521
Mark Brown246d0a12009-04-22 18:24:55 +0100522 if (widget->id == snd_soc_dapm_supply)
523 return 0;
524
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100526 switch (widget->id) {
527 case snd_soc_dapm_dac:
528 case snd_soc_dapm_aif_in:
529 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100530 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100531 default:
532 break;
533 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534
535 if (widget->connected) {
536 /* connected pin ? */
537 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100538 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539
540 /* connected VMID/Bias for lower pops */
541 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100542 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200543
544 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300545 if (widget->id == snd_soc_dapm_mic ||
546 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100547 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200548 }
549
550 list_for_each_entry(path, &widget->sources, list_sink) {
551 if (path->walked)
552 continue;
553
554 if (path->source && path->connect) {
555 path->walked = 1;
556 con += is_connected_input_ep(path->source);
557 }
558 }
559
560 return con;
561}
562
563/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300564 * Handler for generic register modifier widget.
565 */
566int dapm_reg_event(struct snd_soc_dapm_widget *w,
567 struct snd_kcontrol *kcontrol, int event)
568{
569 unsigned int val;
570
571 if (SND_SOC_DAPM_EVENT_ON(event))
572 val = w->on_val;
573 else
574 val = w->off_val;
575
576 snd_soc_update_bits(w->codec, -(w->reg + 1),
577 w->mask << w->shift, val << w->shift);
578
579 return 0;
580}
Mark Brown11589412008-07-29 11:42:23 +0100581EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300582
Mark Brown025756e2009-04-13 11:09:18 +0100583/* Standard power change method, used to apply power changes to most
584 * widgets.
585 */
586static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
587{
588 int ret;
589
590 /* call any power change event handlers */
591 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200592 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756e2009-04-13 11:09:18 +0100593 w->power ? "on" : "off",
594 w->name, w->event_flags);
595
596 /* power up pre event */
597 if (w->power && w->event &&
598 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
599 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
600 if (ret < 0)
601 return ret;
602 }
603
604 /* power down pre event */
605 if (!w->power && w->event &&
606 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
607 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
608 if (ret < 0)
609 return ret;
610 }
611
Mark Brown025756e2009-04-13 11:09:18 +0100612 dapm_update_bits(w);
613
Mark Brown025756e2009-04-13 11:09:18 +0100614 /* power up post event */
615 if (w->power && w->event &&
616 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
617 ret = w->event(w,
618 NULL, SND_SOC_DAPM_POST_PMU);
619 if (ret < 0)
620 return ret;
621 }
622
623 /* power down post event */
624 if (!w->power && w->event &&
625 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
626 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
627 if (ret < 0)
628 return ret;
629 }
630
631 return 0;
632}
633
Mark Browncd0f2d42009-04-20 16:56:59 +0100634/* Generic check to see if a widget should be powered.
635 */
636static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
637{
638 int in, out;
639
640 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200641 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100642 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200643 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100644 return out != 0 && in != 0;
645}
646
Mark Brown6ea31b92009-04-20 17:15:41 +0100647/* Check to see if an ADC has power */
648static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
649{
650 int in;
651
652 if (w->active) {
653 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200654 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100655 return in != 0;
656 } else {
657 return dapm_generic_check_power(w);
658 }
659}
660
661/* Check to see if a DAC has power */
662static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
663{
664 int out;
665
666 if (w->active) {
667 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200668 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100669 return out != 0;
670 } else {
671 return dapm_generic_check_power(w);
672 }
673}
674
Mark Brown246d0a12009-04-22 18:24:55 +0100675/* Check to see if a power supply is needed */
676static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
677{
678 struct snd_soc_dapm_path *path;
679 int power = 0;
680
681 /* Check if one of our outputs is connected */
682 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100683 if (path->connected &&
684 !path->connected(path->source, path->sink))
685 continue;
686
Mark Brown246d0a12009-04-22 18:24:55 +0100687 if (path->sink && path->sink->power_check &&
688 path->sink->power_check(path->sink)) {
689 power = 1;
690 break;
691 }
692 }
693
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200694 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100695
696 return power;
697}
698
Mark Brown38357ab2009-06-06 19:03:23 +0100699static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
700 struct snd_soc_dapm_widget *b,
701 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000702{
Mark Brownd207c682009-12-07 17:13:55 +0000703 if (a->codec != b->codec)
704 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100705 if (sort[a->id] != sort[b->id])
706 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100707 if (a->reg != b->reg)
708 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000709
Mark Brown38357ab2009-06-06 19:03:23 +0100710 return 0;
711}
Mark Brown42aa3412009-03-01 19:21:10 +0000712
Mark Brown38357ab2009-06-06 19:03:23 +0100713/* Insert a widget in order into a DAPM power sequence. */
714static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
715 struct list_head *list,
716 int sort[])
717{
718 struct snd_soc_dapm_widget *w;
719
720 list_for_each_entry(w, list, power_list)
721 if (dapm_seq_compare(new_widget, w, sort) < 0) {
722 list_add_tail(&new_widget->power_list, &w->power_list);
723 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000724 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100725
Mark Brown38357ab2009-06-06 19:03:23 +0100726 list_add_tail(&new_widget->power_list, list);
727}
Mark Brown42aa3412009-03-01 19:21:10 +0000728
Mark Brown68f89ad2010-11-03 23:51:49 -0400729static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
730 struct snd_soc_dapm_widget *w, int event)
731{
732 struct snd_soc_card *card = dapm->card;
733 const char *ev_name;
734 int power, ret;
735
736 switch (event) {
737 case SND_SOC_DAPM_PRE_PMU:
738 ev_name = "PRE_PMU";
739 power = 1;
740 break;
741 case SND_SOC_DAPM_POST_PMU:
742 ev_name = "POST_PMU";
743 power = 1;
744 break;
745 case SND_SOC_DAPM_PRE_PMD:
746 ev_name = "PRE_PMD";
747 power = 0;
748 break;
749 case SND_SOC_DAPM_POST_PMD:
750 ev_name = "POST_PMD";
751 power = 0;
752 break;
753 default:
754 BUG();
755 return;
756 }
757
758 if (w->power != power)
759 return;
760
761 if (w->event && (w->event_flags & event)) {
762 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
763 w->name, ev_name);
764 ret = w->event(w, NULL, event);
765 if (ret < 0)
766 pr_err("%s: %s event failed: %d\n",
767 ev_name, w->name, ret);
768 }
769}
770
Mark Brownb22ead22009-06-07 12:51:26 +0100771/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200772static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100773 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100774{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200775 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400776 struct snd_soc_dapm_widget *w;
777 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100778 unsigned int value = 0;
779 unsigned int mask = 0;
780 unsigned int cur_mask;
781
782 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
783 power_list)->reg;
784
785 list_for_each_entry(w, pending, power_list) {
786 cur_mask = 1 << w->shift;
787 BUG_ON(reg != w->reg);
788
789 if (w->invert)
790 power = !w->power;
791 else
792 power = w->power;
793
794 mask |= cur_mask;
795 if (power)
796 value |= cur_mask;
797
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200798 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100799 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
800 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100801
Mark Brown68f89ad2010-11-03 23:51:49 -0400802 /* Check for events */
803 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
804 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100805 }
806
Mark Brown81628102009-06-07 13:21:24 +0100807 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200808 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100809 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200810 value, mask, reg, card->pop_time);
811 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200812 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100813 }
814
815 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400816 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
817 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000818 }
Mark Brown42aa3412009-03-01 19:21:10 +0000819}
820
Mark Brownb22ead22009-06-07 12:51:26 +0100821/* Apply a DAPM power sequence.
822 *
823 * We walk over a pre-sorted list of widgets to apply power to. In
824 * order to minimise the number of writes to the device required
825 * multiple widgets will be updated in a single write where possible.
826 * Currently anything that requires more than a single write is not
827 * handled.
828 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200829static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
830 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100831{
832 struct snd_soc_dapm_widget *w, *n;
833 LIST_HEAD(pending);
834 int cur_sort = -1;
835 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100836 int ret;
837
Mark Brownb22ead22009-06-07 12:51:26 +0100838 list_for_each_entry_safe(w, n, list, power_list) {
839 ret = 0;
840
841 /* Do we need to apply any queued changes? */
842 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
843 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200844 dapm_seq_run_coalesced(dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100845
846 INIT_LIST_HEAD(&pending);
847 cur_sort = -1;
848 cur_reg = SND_SOC_NOPM;
849 }
850
Mark Brown163cac02009-06-07 10:12:52 +0100851 switch (w->id) {
852 case snd_soc_dapm_pre:
853 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100854 list_for_each_entry_safe_continue(w, n, list,
855 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100856
Mark Brownb22ead22009-06-07 12:51:26 +0100857 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100858 ret = w->event(w,
859 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100860 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100861 ret = w->event(w,
862 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100863 break;
864
865 case snd_soc_dapm_post:
866 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100867 list_for_each_entry_safe_continue(w, n, list,
868 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100869
Mark Brownb22ead22009-06-07 12:51:26 +0100870 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100871 ret = w->event(w,
872 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100873 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100874 ret = w->event(w,
875 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100876 break;
877
878 case snd_soc_dapm_input:
879 case snd_soc_dapm_output:
880 case snd_soc_dapm_hp:
881 case snd_soc_dapm_mic:
882 case snd_soc_dapm_line:
883 case snd_soc_dapm_spk:
884 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100885 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100886 break;
887
888 default:
Mark Brown81628102009-06-07 13:21:24 +0100889 /* Queue it up for application */
890 cur_sort = sort[w->id];
891 cur_reg = w->reg;
892 list_move(&w->power_list, &pending);
893 break;
Mark Brown163cac02009-06-07 10:12:52 +0100894 }
Mark Brownb22ead22009-06-07 12:51:26 +0100895
896 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200897 dev_err(w->dapm->dev,
898 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100899 }
Mark Brownb22ead22009-06-07 12:51:26 +0100900
901 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200902 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100903}
904
Mark Brown42aa3412009-03-01 19:21:10 +0000905/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200906 * Scan each dapm widget for complete audio path.
907 * A complete path is a route that has valid endpoints i.e.:-
908 *
909 * o DAC to output pin.
910 * o Input Pin to ADC.
911 * o Input pin to Output pin (bypass, sidetone)
912 * o DAC to ADC (loopback).
913 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200914static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200916 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100918 LIST_HEAD(up_list);
919 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100920 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100921 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100922 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923
Mark Brown6d3ddc82009-05-16 17:47:29 +0100924 /* Check which widgets we need to power and store them in
925 * lists indicating if they should be powered up or down.
926 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200927 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +0100928 switch (w->id) {
929 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100930 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100931 break;
932 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100933 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100934 break;
935
936 default:
937 if (!w->power_check)
938 continue;
939
Mark Brown99497882010-05-07 20:24:05 +0100940 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +0000941 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +0100942 else
943 power = 1;
944 if (power)
945 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100946
Mark Brown6d3ddc82009-05-16 17:47:29 +0100947 if (w->power == power)
948 continue;
949
950 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100951 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100952 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100953 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100954
955 w->power = power;
956 break;
957 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958 }
959
Mark Brownb14b76a2009-08-17 11:55:38 +0100960 /* If there are no DAPM widgets then try to figure out power from the
961 * event type.
962 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200963 if (list_empty(&dapm->widgets)) {
Mark Brownb14b76a2009-08-17 11:55:38 +0100964 switch (event) {
965 case SND_SOC_DAPM_STREAM_START:
966 case SND_SOC_DAPM_STREAM_RESUME:
967 sys_power = 1;
968 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000969 case SND_SOC_DAPM_STREAM_SUSPEND:
970 sys_power = 0;
971 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100972 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200973 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +0000974 case SND_SOC_BIAS_STANDBY:
975 case SND_SOC_BIAS_OFF:
976 sys_power = 0;
977 break;
978 default:
979 sys_power = 1;
980 break;
981 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000982 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100983 default:
984 break;
985 }
986 }
987
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200988 if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
989 ret = snd_soc_dapm_set_bias_level(card, dapm,
Mark Browna96ca332010-01-19 22:49:43 +0000990 SND_SOC_BIAS_STANDBY);
991 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200992 dev_err(dapm->dev,
993 "Failed to turn on bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +0000994 }
995
Mark Brown452c5ea2009-05-17 21:41:23 +0100996 /* If we're changing to all on or all off then prepare */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200997 if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
998 (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
999 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
Mark Brown452c5ea2009-05-17 21:41:23 +01001000 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001001 dev_err(dapm->dev,
1002 "Failed to prepare bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001003 }
1004
Mark Brown6d3ddc82009-05-16 17:47:29 +01001005 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001006 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001007
1008 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001009 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010
Mark Brown452c5ea2009-05-17 21:41:23 +01001011 /* If we just powered the last thing off drop to standby bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001012 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1013 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown452c5ea2009-05-17 21:41:23 +01001014 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001015 dev_err(dapm->dev,
1016 "Failed to apply standby bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001017 }
1018
Mark Browna96ca332010-01-19 22:49:43 +00001019 /* If we're in standby and can support bias off then do that */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001020 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
1021 dapm->idle_bias_off) {
1022 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
Mark Browna96ca332010-01-19 22:49:43 +00001023 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001024 dev_err(dapm->dev,
1025 "Failed to turn off bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +00001026 }
1027
Mark Brown452c5ea2009-05-17 21:41:23 +01001028 /* If we just powered up then move to active bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001029 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1030 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
Mark Brown452c5ea2009-05-17 21:41:23 +01001031 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001032 dev_err(dapm->dev,
1033 "Failed to apply active bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001034 }
1035
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001036 pop_dbg(dapm->dev, card->pop_time,
1037 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001038 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001039
Mark Brown42aa3412009-03-01 19:21:10 +00001040 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041}
1042
Mark Brown79fb9382009-08-21 16:38:13 +01001043#ifdef CONFIG_DEBUG_FS
1044static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1045{
1046 file->private_data = inode->i_private;
1047 return 0;
1048}
1049
1050static ssize_t dapm_widget_power_read_file(struct file *file,
1051 char __user *user_buf,
1052 size_t count, loff_t *ppos)
1053{
1054 struct snd_soc_dapm_widget *w = file->private_data;
1055 char *buf;
1056 int in, out;
1057 ssize_t ret;
1058 struct snd_soc_dapm_path *p = NULL;
1059
1060 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1061 if (!buf)
1062 return -ENOMEM;
1063
1064 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001065 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001066 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001067 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001068
Mark Brownd033c362009-12-04 15:25:56 +00001069 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001070 w->name, w->power ? "On" : "Off", in, out);
1071
Mark Brownd033c362009-12-04 15:25:56 +00001072 if (w->reg >= 0)
1073 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1074 " - R%d(0x%x) bit %d",
1075 w->reg, w->reg, w->shift);
1076
1077 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1078
Mark Brown3eef08b2009-09-14 16:49:00 +01001079 if (w->sname)
1080 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1081 w->sname,
1082 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001083
1084 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001085 if (p->connected && !p->connected(w, p->sink))
1086 continue;
1087
Mark Brown79fb9382009-08-21 16:38:13 +01001088 if (p->connect)
1089 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1090 " in %s %s\n",
1091 p->name ? p->name : "static",
1092 p->source->name);
1093 }
1094 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001095 if (p->connected && !p->connected(w, p->sink))
1096 continue;
1097
Mark Brown79fb9382009-08-21 16:38:13 +01001098 if (p->connect)
1099 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1100 " out %s %s\n",
1101 p->name ? p->name : "static",
1102 p->sink->name);
1103 }
1104
1105 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1106
1107 kfree(buf);
1108 return ret;
1109}
1110
1111static const struct file_operations dapm_widget_power_fops = {
1112 .open = dapm_widget_power_open_file,
1113 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001114 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001115};
1116
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001117void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001118{
1119 struct snd_soc_dapm_widget *w;
1120 struct dentry *d;
1121
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001122 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001123 return;
1124
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001125 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown79fb9382009-08-21 16:38:13 +01001126 if (!w->name)
1127 continue;
1128
1129 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001130 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001131 &dapm_widget_power_fops);
1132 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001133 dev_warn(w->dapm->dev,
1134 "ASoC: Failed to create %s debugfs file\n",
1135 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001136 }
1137}
1138#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001139void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001140{
1141}
1142#endif
1143
Richard Purdie2b97eab2006-10-06 18:32:18 +02001144/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001145static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001146 struct snd_kcontrol *kcontrol, int change,
1147 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001148{
1149 struct snd_soc_dapm_path *path;
1150 int found = 0;
1151
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001152 if (widget->id != snd_soc_dapm_mux &&
1153 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154 return -ENODEV;
1155
Mark Brown3a655772009-10-05 17:23:30 +01001156 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157 return 0;
1158
1159 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001160 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001161 if (path->kcontrol != kcontrol)
1162 continue;
1163
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001164 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001165 continue;
1166
1167 found = 1;
1168 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001169 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170 path->connect = 1; /* new connection */
1171 else
1172 path->connect = 0; /* old connection must be powered down */
1173 }
1174
Mark Brownb91b8fa2010-01-20 18:18:35 +00001175 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001176 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001177
1178 return 0;
1179}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001180
Milan plzik1b075e32008-01-10 14:39:46 +01001181/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001182static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001183 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001184{
1185 struct snd_soc_dapm_path *path;
1186 int found = 0;
1187
Milan plzik1b075e32008-01-10 14:39:46 +01001188 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001189 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001190 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001191 return -ENODEV;
1192
Richard Purdie2b97eab2006-10-06 18:32:18 +02001193 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001194 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 if (path->kcontrol != kcontrol)
1196 continue;
1197
1198 /* found, now check type */
1199 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001200 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001201 break;
1202 }
1203
Mark Brownb91b8fa2010-01-20 18:18:35 +00001204 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001205 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206
1207 return 0;
1208}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209
1210/* show dapm widget status in sys fs */
1211static ssize_t dapm_widget_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1213{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001214 struct snd_soc_pcm_runtime *rtd =
1215 container_of(dev, struct snd_soc_pcm_runtime, dev);
1216 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 struct snd_soc_dapm_widget *w;
1218 int count = 0;
1219 char *state = "not set";
1220
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001221 list_for_each_entry(w, &codec->dapm.widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001222
1223 /* only display widgets that burnm power */
1224 switch (w->id) {
1225 case snd_soc_dapm_hp:
1226 case snd_soc_dapm_mic:
1227 case snd_soc_dapm_spk:
1228 case snd_soc_dapm_line:
1229 case snd_soc_dapm_micbias:
1230 case snd_soc_dapm_dac:
1231 case snd_soc_dapm_adc:
1232 case snd_soc_dapm_pga:
1233 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001234 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001235 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236 if (w->name)
1237 count += sprintf(buf + count, "%s: %s\n",
1238 w->name, w->power ? "On":"Off");
1239 break;
1240 default:
1241 break;
1242 }
1243 }
1244
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001245 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001246 case SND_SOC_BIAS_ON:
1247 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001248 break;
Mark Brown0be98982008-05-19 12:31:28 +02001249 case SND_SOC_BIAS_PREPARE:
1250 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001251 break;
Mark Brown0be98982008-05-19 12:31:28 +02001252 case SND_SOC_BIAS_STANDBY:
1253 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001254 break;
Mark Brown0be98982008-05-19 12:31:28 +02001255 case SND_SOC_BIAS_OFF:
1256 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001257 break;
1258 }
1259 count += sprintf(buf + count, "PM State: %s\n", state);
1260
1261 return count;
1262}
1263
1264static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1265
1266int snd_soc_dapm_sys_add(struct device *dev)
1267{
Troy Kisky12ef1932008-10-13 17:42:14 -07001268 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001269}
1270
1271static void snd_soc_dapm_sys_remove(struct device *dev)
1272{
Mark Brownaef90842009-05-16 17:53:16 +01001273 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001274}
1275
1276/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001277static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001278{
1279 struct snd_soc_dapm_widget *w, *next_w;
1280 struct snd_soc_dapm_path *p, *next_p;
1281
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001282 list_for_each_entry_safe(w, next_w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001283 list_del(&w->list);
1284 kfree(w);
1285 }
1286
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001287 list_for_each_entry_safe(p, next_p, &dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001288 list_del(&p->list);
1289 kfree(p->long_name);
1290 kfree(p);
1291 }
1292}
1293
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001294static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001295 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001296{
1297 struct snd_soc_dapm_widget *w;
1298
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001299 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001300 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001301 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1302 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001303 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001304 /* Allow disabling of forced pins */
1305 if (status == 0)
1306 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001307 return 0;
1308 }
1309 }
1310
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001311 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001312 return -EINVAL;
1313}
1314
Richard Purdie2b97eab2006-10-06 18:32:18 +02001315/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001316 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001317 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001318 *
1319 * Walks all dapm audio paths and powers widgets according to their
1320 * stream or path usage.
1321 *
1322 * Returns 0 for success.
1323 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001324int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001325{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001326 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001327}
Liam Girdwooda5302182008-07-07 13:35:17 +01001328EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001329
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001330static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001331 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001332{
1333 struct snd_soc_dapm_path *path;
1334 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001335 const char *sink = route->sink;
1336 const char *control = route->control;
1337 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001338 int ret = 0;
1339
1340 /* find src and dest widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001341 list_for_each_entry(w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342
1343 if (!wsink && !(strcmp(w->name, sink))) {
1344 wsink = w;
1345 continue;
1346 }
1347 if (!wsource && !(strcmp(w->name, source))) {
1348 wsource = w;
1349 }
1350 }
1351
1352 if (wsource == NULL || wsink == NULL)
1353 return -ENODEV;
1354
1355 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1356 if (!path)
1357 return -ENOMEM;
1358
1359 path->source = wsource;
1360 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001361 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001362 INIT_LIST_HEAD(&path->list);
1363 INIT_LIST_HEAD(&path->list_source);
1364 INIT_LIST_HEAD(&path->list_sink);
1365
1366 /* check for external widgets */
1367 if (wsink->id == snd_soc_dapm_input) {
1368 if (wsource->id == snd_soc_dapm_micbias ||
1369 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001370 wsource->id == snd_soc_dapm_line ||
1371 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001372 wsink->ext = 1;
1373 }
1374 if (wsource->id == snd_soc_dapm_output) {
1375 if (wsink->id == snd_soc_dapm_spk ||
1376 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001377 wsink->id == snd_soc_dapm_line ||
1378 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001379 wsource->ext = 1;
1380 }
1381
1382 /* connect static paths */
1383 if (control == NULL) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001384 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001385 list_add(&path->list_sink, &wsink->sources);
1386 list_add(&path->list_source, &wsource->sinks);
1387 path->connect = 1;
1388 return 0;
1389 }
1390
1391 /* connect dynamic paths */
1392 switch(wsink->id) {
1393 case snd_soc_dapm_adc:
1394 case snd_soc_dapm_dac:
1395 case snd_soc_dapm_pga:
1396 case snd_soc_dapm_input:
1397 case snd_soc_dapm_output:
1398 case snd_soc_dapm_micbias:
1399 case snd_soc_dapm_vmid:
1400 case snd_soc_dapm_pre:
1401 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001402 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001403 case snd_soc_dapm_aif_in:
1404 case snd_soc_dapm_aif_out:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001405 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001406 list_add(&path->list_sink, &wsink->sources);
1407 list_add(&path->list_source, &wsource->sinks);
1408 path->connect = 1;
1409 return 0;
1410 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001411 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001412 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413 &wsink->kcontrols[0]);
1414 if (ret != 0)
1415 goto err;
1416 break;
1417 case snd_soc_dapm_switch:
1418 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001419 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001420 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001421 if (ret != 0)
1422 goto err;
1423 break;
1424 case snd_soc_dapm_hp:
1425 case snd_soc_dapm_mic:
1426 case snd_soc_dapm_line:
1427 case snd_soc_dapm_spk:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001428 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001429 list_add(&path->list_sink, &wsink->sources);
1430 list_add(&path->list_source, &wsource->sinks);
1431 path->connect = 0;
1432 return 0;
1433 }
1434 return 0;
1435
1436err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001437 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1438 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439 kfree(path);
1440 return ret;
1441}
Mark Brown105f1c22008-05-13 14:52:19 +02001442
1443/**
Mark Brown105f1c22008-05-13 14:52:19 +02001444 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001445 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001446 * @route: audio routes
1447 * @num: number of routes
1448 *
1449 * Connects 2 dapm widgets together via a named audio path. The sink is
1450 * the widget receiving the audio signal, whilst the source is the sender
1451 * of the audio signal.
1452 *
1453 * Returns 0 for success else error. On error all resources can be freed
1454 * with a call to snd_soc_card_free().
1455 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001456int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001457 const struct snd_soc_dapm_route *route, int num)
1458{
1459 int i, ret;
1460
1461 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001462 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001463 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001464 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1465 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001466 return ret;
1467 }
1468 route++;
1469 }
1470
1471 return 0;
1472}
1473EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1474
1475/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001476 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001477 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001478 *
1479 * Checks the codec for any new dapm widgets and creates them if found.
1480 *
1481 * Returns 0 for success.
1482 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001483int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484{
1485 struct snd_soc_dapm_widget *w;
1486
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001487 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001488 {
1489 if (w->new)
1490 continue;
1491
1492 switch(w->id) {
1493 case snd_soc_dapm_switch:
1494 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001495 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001496 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001497 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001498 break;
1499 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001500 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001501 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001502 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001503 break;
1504 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001505 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001506 w->power_check = dapm_adc_check_power;
1507 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001508 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001509 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001510 w->power_check = dapm_dac_check_power;
1511 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001512 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001513 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001514 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001515 break;
1516 case snd_soc_dapm_input:
1517 case snd_soc_dapm_output:
1518 case snd_soc_dapm_micbias:
1519 case snd_soc_dapm_spk:
1520 case snd_soc_dapm_hp:
1521 case snd_soc_dapm_mic:
1522 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001523 w->power_check = dapm_generic_check_power;
1524 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001525 case snd_soc_dapm_supply:
1526 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001527 case snd_soc_dapm_vmid:
1528 case snd_soc_dapm_pre:
1529 case snd_soc_dapm_post:
1530 break;
1531 }
1532 w->new = 1;
1533 }
1534
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001535 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536 return 0;
1537}
1538EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1539
1540/**
1541 * snd_soc_dapm_get_volsw - dapm mixer get callback
1542 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001543 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544 *
1545 * Callback to get the value of a dapm mixer control.
1546 *
1547 * Returns 0 for success.
1548 */
1549int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_value *ucontrol)
1551{
1552 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001553 struct soc_mixer_control *mc =
1554 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001555 unsigned int reg = mc->reg;
1556 unsigned int shift = mc->shift;
1557 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001558 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001559 unsigned int invert = mc->invert;
1560 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001561
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 ucontrol->value.integer.value[0] =
1563 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1564 if (shift != rshift)
1565 ucontrol->value.integer.value[1] =
1566 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1567 if (invert) {
1568 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001569 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001570 if (shift != rshift)
1571 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001572 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 }
1574
1575 return 0;
1576}
1577EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1578
1579/**
1580 * snd_soc_dapm_put_volsw - dapm mixer set callback
1581 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001582 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001583 *
1584 * Callback to set the value of a dapm mixer control.
1585 *
1586 * Returns 0 for success.
1587 */
1588int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
1590{
1591 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001592 struct soc_mixer_control *mc =
1593 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001594 unsigned int reg = mc->reg;
1595 unsigned int shift = mc->shift;
1596 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001597 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001598 unsigned int mask = (1 << fls(max)) - 1;
1599 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001600 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001601 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602 int ret;
1603
1604 val = (ucontrol->value.integer.value[0] & mask);
1605
1606 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001607 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608 val_mask = mask << shift;
1609 val = val << shift;
1610 if (shift != rshift) {
1611 val2 = (ucontrol->value.integer.value[1] & mask);
1612 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001613 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001614 val_mask |= mask << rshift;
1615 val |= val2 << rshift;
1616 }
1617
1618 mutex_lock(&widget->codec->mutex);
1619 widget->value = val;
1620
Mark Brown283375c2009-12-07 18:09:03 +00001621 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1622 if (val)
1623 /* new connection */
1624 connect = invert ? 0:1;
1625 else
1626 /* old connection must be powered down */
1627 connect = invert ? 1:0;
1628
1629 dapm_mixer_update_power(widget, kcontrol, connect);
1630 }
1631
Richard Purdie2b97eab2006-10-06 18:32:18 +02001632 if (widget->event) {
1633 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001634 ret = widget->event(widget, kcontrol,
1635 SND_SOC_DAPM_PRE_REG);
1636 if (ret < 0) {
1637 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001638 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001639 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001640 }
1641 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1642 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001643 ret = widget->event(widget, kcontrol,
1644 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001645 } else
1646 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1647
1648out:
1649 mutex_unlock(&widget->codec->mutex);
1650 return ret;
1651}
1652EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1653
1654/**
1655 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1656 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001657 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 *
1659 * Callback to get the value of a dapm enumerated double mixer control.
1660 *
1661 * Returns 0 for success.
1662 */
1663int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
1665{
1666 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1667 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001668 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001669
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001670 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 ;
1672 val = snd_soc_read(widget->codec, e->reg);
1673 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1674 if (e->shift_l != e->shift_r)
1675 ucontrol->value.enumerated.item[1] =
1676 (val >> e->shift_r) & (bitmask - 1);
1677
1678 return 0;
1679}
1680EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1681
1682/**
1683 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1684 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001685 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001686 *
1687 * Callback to set the value of a dapm enumerated double mixer control.
1688 *
1689 * Returns 0 for success.
1690 */
1691int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
1693{
1694 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1695 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001696 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001697 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698 int ret = 0;
1699
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001700 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001701 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001702 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 return -EINVAL;
1704 mux = ucontrol->value.enumerated.item[0];
1705 val = mux << e->shift_l;
1706 mask = (bitmask - 1) << e->shift_l;
1707 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001708 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001709 return -EINVAL;
1710 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1711 mask |= (bitmask - 1) << e->shift_r;
1712 }
1713
1714 mutex_lock(&widget->codec->mutex);
1715 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001716 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1717 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001718
1719 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1720 ret = widget->event(widget,
1721 kcontrol, SND_SOC_DAPM_PRE_REG);
1722 if (ret < 0)
1723 goto out;
1724 }
1725
1726 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1727
1728 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1729 ret = widget->event(widget,
1730 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001731
1732out:
1733 mutex_unlock(&widget->codec->mutex);
1734 return ret;
1735}
1736EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1737
1738/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001739 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1740 * @kcontrol: mixer control
1741 * @ucontrol: control element information
1742 *
1743 * Returns 0 for success.
1744 */
1745int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *ucontrol)
1747{
1748 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1749
1750 ucontrol->value.enumerated.item[0] = widget->value;
1751
1752 return 0;
1753}
1754EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1755
1756/**
1757 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1758 * @kcontrol: mixer control
1759 * @ucontrol: control element information
1760 *
1761 * Returns 0 for success.
1762 */
1763int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1764 struct snd_ctl_elem_value *ucontrol)
1765{
1766 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1767 struct soc_enum *e =
1768 (struct soc_enum *)kcontrol->private_value;
1769 int change;
1770 int ret = 0;
1771
1772 if (ucontrol->value.enumerated.item[0] >= e->max)
1773 return -EINVAL;
1774
1775 mutex_lock(&widget->codec->mutex);
1776
1777 change = widget->value != ucontrol->value.enumerated.item[0];
1778 widget->value = ucontrol->value.enumerated.item[0];
1779 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1780
1781 mutex_unlock(&widget->codec->mutex);
1782 return ret;
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1785
1786/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001787 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1788 * callback
1789 * @kcontrol: mixer control
1790 * @ucontrol: control element information
1791 *
1792 * Callback to get the value of a dapm semi enumerated double mixer control.
1793 *
1794 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1795 * used for handling bitfield coded enumeration for example.
1796 *
1797 * Returns 0 for success.
1798 */
1799int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_value *ucontrol)
1801{
1802 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001803 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001804 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001805
1806 reg_val = snd_soc_read(widget->codec, e->reg);
1807 val = (reg_val >> e->shift_l) & e->mask;
1808 for (mux = 0; mux < e->max; mux++) {
1809 if (val == e->values[mux])
1810 break;
1811 }
1812 ucontrol->value.enumerated.item[0] = mux;
1813 if (e->shift_l != e->shift_r) {
1814 val = (reg_val >> e->shift_r) & e->mask;
1815 for (mux = 0; mux < e->max; mux++) {
1816 if (val == e->values[mux])
1817 break;
1818 }
1819 ucontrol->value.enumerated.item[1] = mux;
1820 }
1821
1822 return 0;
1823}
1824EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1825
1826/**
1827 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1828 * callback
1829 * @kcontrol: mixer control
1830 * @ucontrol: control element information
1831 *
1832 * Callback to set the value of a dapm semi enumerated double mixer control.
1833 *
1834 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1835 * used for handling bitfield coded enumeration for example.
1836 *
1837 * Returns 0 for success.
1838 */
1839int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1840 struct snd_ctl_elem_value *ucontrol)
1841{
1842 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001843 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001844 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001845 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001846 int ret = 0;
1847
1848 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1849 return -EINVAL;
1850 mux = ucontrol->value.enumerated.item[0];
1851 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1852 mask = e->mask << e->shift_l;
1853 if (e->shift_l != e->shift_r) {
1854 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1855 return -EINVAL;
1856 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1857 mask |= e->mask << e->shift_r;
1858 }
1859
1860 mutex_lock(&widget->codec->mutex);
1861 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001862 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1863 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001864
1865 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1866 ret = widget->event(widget,
1867 kcontrol, SND_SOC_DAPM_PRE_REG);
1868 if (ret < 0)
1869 goto out;
1870 }
1871
1872 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1873
1874 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1875 ret = widget->event(widget,
1876 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001877
1878out:
1879 mutex_unlock(&widget->codec->mutex);
1880 return ret;
1881}
1882EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1883
1884/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001885 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1886 *
1887 * @kcontrol: mixer control
1888 * @uinfo: control element information
1889 *
1890 * Callback to provide information about a pin switch control.
1891 */
1892int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_info *uinfo)
1894{
1895 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1896 uinfo->count = 1;
1897 uinfo->value.integer.min = 0;
1898 uinfo->value.integer.max = 1;
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1903
1904/**
1905 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1906 *
1907 * @kcontrol: mixer control
1908 * @ucontrol: Value
1909 */
1910int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_value *ucontrol)
1912{
1913 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1914 const char *pin = (const char *)kcontrol->private_value;
1915
1916 mutex_lock(&codec->mutex);
1917
1918 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001919 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001920
1921 mutex_unlock(&codec->mutex);
1922
1923 return 0;
1924}
1925EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1926
1927/**
1928 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1929 *
1930 * @kcontrol: mixer control
1931 * @ucontrol: Value
1932 */
1933int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *ucontrol)
1935{
1936 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1937 const char *pin = (const char *)kcontrol->private_value;
1938
1939 mutex_lock(&codec->mutex);
1940
1941 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001942 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001943 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001944 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001945
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001946 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001947
1948 mutex_unlock(&codec->mutex);
1949
1950 return 0;
1951}
1952EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1953
1954/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001955 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001956 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957 * @widget: widget template
1958 *
1959 * Creates a new dapm control based upon the template.
1960 *
1961 * Returns 0 for success else error.
1962 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001963int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964 const struct snd_soc_dapm_widget *widget)
1965{
1966 struct snd_soc_dapm_widget *w;
1967
1968 if ((w = dapm_cnew_widget(widget)) == NULL)
1969 return -ENOMEM;
1970
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001971 w->dapm = dapm;
1972 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973 INIT_LIST_HEAD(&w->sources);
1974 INIT_LIST_HEAD(&w->sinks);
1975 INIT_LIST_HEAD(&w->list);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001976 list_add(&w->list, &dapm->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001977
1978 /* machine layer set ups unconnected pins and insertions */
1979 w->connected = 1;
1980 return 0;
1981}
1982EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1983
1984/**
Mark Brown4ba13272008-05-13 14:51:19 +02001985 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001986 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02001987 * @widget: widget array
1988 * @num: number of widgets
1989 *
1990 * Creates new DAPM controls based upon the templates.
1991 *
1992 * Returns 0 for success else error.
1993 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001994int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02001995 const struct snd_soc_dapm_widget *widget,
1996 int num)
1997{
1998 int i, ret;
1999
2000 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002001 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002002 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002003 dev_err(dapm->dev,
2004 "ASoC: Failed to create DAPM control %s: %d\n",
2005 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002006 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002007 }
Mark Brown4ba13272008-05-13 14:51:19 +02002008 widget++;
2009 }
2010 return 0;
2011}
2012EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2013
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002014static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002015 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016{
2017 struct snd_soc_dapm_widget *w;
2018
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002019 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020 {
2021 if (!w->sname)
2022 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002023 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2024 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025 if (strstr(w->sname, stream)) {
2026 switch(event) {
2027 case SND_SOC_DAPM_STREAM_START:
2028 w->active = 1;
2029 break;
2030 case SND_SOC_DAPM_STREAM_STOP:
2031 w->active = 0;
2032 break;
2033 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2037 break;
2038 }
2039 }
2040 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002042 dapm_power_widgets(dapm, event);
2043}
2044
2045/**
2046 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2047 * @rtd: PCM runtime data
2048 * @stream: stream name
2049 * @event: stream event
2050 *
2051 * Sends a stream event to the dapm core. The core then makes any
2052 * necessary widget power changes.
2053 *
2054 * Returns 0 for success else error.
2055 */
2056int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2057 const char *stream, int event)
2058{
2059 struct snd_soc_codec *codec = rtd->codec;
2060
2061 if (stream == NULL)
2062 return 0;
2063
2064 mutex_lock(&codec->mutex);
2065 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002066 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 return 0;
2068}
2069EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2070
2071/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002072 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002073 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002074 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 *
Mark Brown74b8f952009-06-06 11:26:15 +01002076 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002077 * a valid audio route and active audio stream.
2078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2079 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002081int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002083 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084}
Liam Girdwooda5302182008-07-07 13:35:17 +01002085EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002086
2087/**
Mark Brownda341832010-03-15 19:23:37 +00002088 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002089 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002090 * @pin: pin name
2091 *
2092 * Enables input/output pin regardless of any other state. This is
2093 * intended for use with microphone bias supplies used in microphone
2094 * jack detection.
2095 *
2096 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2097 * do any widget power switching.
2098 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002099int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2100 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002101{
2102 struct snd_soc_dapm_widget *w;
2103
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002104 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brownda341832010-03-15 19:23:37 +00002105 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002106 dev_dbg(w->dapm->dev,
2107 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002108 w->connected = 1;
2109 w->force = 1;
2110 return 0;
2111 }
2112 }
2113
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002114 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002115 return -EINVAL;
2116}
2117EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2118
2119/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002120 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002121 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002122 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002123 *
Mark Brown74b8f952009-06-06 11:26:15 +01002124 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002125 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2126 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002127 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002128int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2129 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002130{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002131 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002132}
2133EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2134
2135/**
Mark Brown5817b522008-09-24 11:23:11 +01002136 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002137 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002138 * @pin: pin name
2139 *
2140 * Marks the specified pin as being not connected, disabling it along
2141 * any parent or child widgets. At present this is identical to
2142 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2143 * additional things such as disabling controls which only affect
2144 * paths through the pin.
2145 *
2146 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2147 * do any widget power switching.
2148 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002149int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002150{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002151 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002152}
2153EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2154
2155/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002156 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002157 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002158 * @pin: audio signal pin endpoint (or start point)
2159 *
2160 * Get audio pin status - connected or disconnected.
2161 *
2162 * Returns 1 for connected otherwise 0.
2163 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002164int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2165 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002166{
2167 struct snd_soc_dapm_widget *w;
2168
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002169 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002170 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002171 return w->connected;
2172 }
2173
2174 return 0;
2175}
Liam Girdwooda5302182008-07-07 13:35:17 +01002176EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002177
2178/**
Mark Brown1547aba2010-05-07 21:11:40 +01002179 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002180 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002181 * @pin: audio signal pin endpoint (or start point)
2182 *
2183 * Mark the given endpoint or pin as ignoring suspend. When the
2184 * system is disabled a path between two endpoints flagged as ignoring
2185 * suspend will not be disabled. The path must already be enabled via
2186 * normal means at suspend time, it will not be turned on if it was not
2187 * already enabled.
2188 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2190 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002191{
2192 struct snd_soc_dapm_widget *w;
2193
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown1547aba2010-05-07 21:11:40 +01002195 if (!strcmp(w->name, pin)) {
2196 w->ignore_suspend = 1;
2197 return 0;
2198 }
2199 }
2200
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002201 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002202 return -EINVAL;
2203}
2204EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2205
2206/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002208 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209 *
2210 * Free all dapm widgets and resources.
2211 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002212void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002214 snd_soc_dapm_sys_remove(dapm->dev);
2215 dapm_free_widgets(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216}
2217EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2218
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002219static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002220{
Mark Brown51737472009-06-22 13:16:51 +01002221 struct snd_soc_dapm_widget *w;
2222 LIST_HEAD(down_list);
2223 int powerdown = 0;
2224
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002225 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown51737472009-06-22 13:16:51 +01002226 if (w->power) {
2227 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002228 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002229 powerdown = 1;
2230 }
2231 }
2232
2233 /* If there were no widgets to power down we're already in
2234 * standby.
2235 */
2236 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002237 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2238 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2239 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002240 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002241}
Mark Brown51737472009-06-22 13:16:51 +01002242
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002243/*
2244 * snd_soc_dapm_shutdown - callback for system shutdown
2245 */
2246void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2247{
2248 struct snd_soc_codec *codec;
2249
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002250 list_for_each_entry(codec, &card->codec_dev_list, list) {
2251 soc_dapm_shutdown_codec(&codec->dapm);
2252 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2253 }
Mark Brown51737472009-06-22 13:16:51 +01002254}
2255
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002257MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2259MODULE_LICENSE("GPL");