blob: 0d8b08ef873170fa5162c26f0cf35b93c69100c1 [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>
Richard Purdie2b97eab2006-10-06 18:32:18 +020041#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc-dapm.h>
45#include <sound/initval.h>
46
47/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010048#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020050#else
51#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020052#endif
53
Richard Purdie2b97eab2006-10-06 18:32:18 +020054/* dapm power sequences - make this per codec in the future */
55static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_pre] = 0,
57 [snd_soc_dapm_supply] = 1,
58 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
63 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
69 [snd_soc_dapm_hp] = 10,
70 [snd_soc_dapm_spk] = 10,
71 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020072};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000073
Richard Purdie2b97eab2006-10-06 18:32:18 +020074static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010075 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010078 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010087 [snd_soc_dapm_aif_in] = 10,
88 [snd_soc_dapm_aif_out] = 10,
89 [snd_soc_dapm_supply] = 11,
90 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020091};
92
Troy Kisky12ef1932008-10-13 17:42:14 -070093static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010094{
95 if (pop_time)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97}
98
Troy Kisky12ef1932008-10-13 17:42:14 -070099static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100100{
101 va_list args;
102
103 va_start(args, fmt);
104
105 if (pop_time) {
106 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700107 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100108 }
109
110 va_end(args);
111}
112
Richard Purdie2b97eab2006-10-06 18:32:18 +0200113/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100114static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200115 const struct snd_soc_dapm_widget *_widget)
116{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100117 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200118}
119
Mark Brown452c5ea2009-05-17 21:41:23 +0100120/**
121 * snd_soc_dapm_set_bias_level - set the bias level for the system
122 * @socdev: audio device
123 * @level: level to configure
124 *
125 * Configure the bias (power) levels for the SoC audio device.
126 *
127 * Returns 0 for success else error.
128 */
129static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
130 enum snd_soc_bias_level level)
131{
132 struct snd_soc_card *card = socdev->card;
133 struct snd_soc_codec *codec = socdev->card->codec;
134 int ret = 0;
135
Mark Brownf83fba82009-05-18 15:44:43 +0100136 switch (level) {
137 case SND_SOC_BIAS_ON:
138 dev_dbg(socdev->dev, "Setting full bias\n");
139 break;
140 case SND_SOC_BIAS_PREPARE:
141 dev_dbg(socdev->dev, "Setting bias prepare\n");
142 break;
143 case SND_SOC_BIAS_STANDBY:
144 dev_dbg(socdev->dev, "Setting standby bias\n");
145 break;
146 case SND_SOC_BIAS_OFF:
147 dev_dbg(socdev->dev, "Setting bias off\n");
148 break;
149 default:
150 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
151 return -EINVAL;
152 }
153
Mark Brown452c5ea2009-05-17 21:41:23 +0100154 if (card->set_bias_level)
155 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100156 if (ret == 0) {
157 if (codec->set_bias_level)
158 ret = codec->set_bias_level(codec, level);
159 else
160 codec->bias_level = level;
161 }
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 */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200253static int dapm_connect_mux(struct snd_soc_codec *codec,
254 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]))) {
263 list_add(&path->list, &codec->dapm_paths);
264 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 */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276static int dapm_connect_mixer(struct snd_soc_codec *codec,
277 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)) {
285 list_add(&path->list, &codec->dapm_paths);
286 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;
302
303 /* check for valid widgets */
304 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
305 widget->id == snd_soc_dapm_output ||
306 widget->id == snd_soc_dapm_hp ||
307 widget->id == snd_soc_dapm_mic ||
308 widget->id == snd_soc_dapm_line ||
309 widget->id == snd_soc_dapm_spk)
310 return 0;
311
312 power = widget->power;
313 if (widget->invert)
314 power = (power ? 0:1);
315
316 old = snd_soc_read(codec, widget->reg);
317 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
318
319 change = old != new;
320 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700321 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
322 widget->name, widget->power ? "on" : "off",
323 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200324 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700325 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200326 }
Mark Brownc1286b82008-07-07 19:26:03 +0100327 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
328 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 return change;
330}
331
332/* ramps the volume up or down to minimise pops before or after a
333 * DAPM power event */
334static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
335{
336 const struct snd_kcontrol_new *k = widget->kcontrols;
337
338 if (widget->muted && !power)
339 return 0;
340 if (!widget->muted && power)
341 return 0;
342
343 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100344 struct soc_mixer_control *mc =
345 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400346 unsigned int reg = mc->reg;
347 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100348 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400349 unsigned int mask = (1 << fls(max)) - 1;
350 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351
352 if (power) {
353 int i;
354 /* power up has happended, increase volume to last level */
355 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100356 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 snd_soc_update_bits(widget->codec, reg, mask, i);
358 } else {
359 for (i = 0; i < widget->saved_value; i++)
360 snd_soc_update_bits(widget->codec, reg, mask, i);
361 }
362 widget->muted = 0;
363 } else {
364 /* power down is about to occur, decrease volume to mute */
365 int val = snd_soc_read(widget->codec, reg);
366 int i = widget->saved_value = (val >> shift) & mask;
367 if (invert) {
368 for (; i < mask; i++)
369 snd_soc_update_bits(widget->codec, reg, mask, i);
370 } else {
371 for (; i > 0; i--)
372 snd_soc_update_bits(widget->codec, reg, mask, i);
373 }
374 widget->muted = 1;
375 }
376 }
377 return 0;
378}
379
380/* create new dapm mixer control */
381static int dapm_new_mixer(struct snd_soc_codec *codec,
382 struct snd_soc_dapm_widget *w)
383{
384 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000385 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386 struct snd_soc_dapm_path *path;
387
388 /* add kcontrol */
389 for (i = 0; i < w->num_kcontrols; i++) {
390
391 /* match name */
392 list_for_each_entry(path, &w->sources, list_sink) {
393
394 /* mixer/mux paths name must match control name */
395 if (path->name != (char*)w->kcontrols[i].name)
396 continue;
397
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 /* add dapm control with long name.
399 * for dapm_mixer this is the concatenation of the
400 * mixer and kcontrol name.
401 * for dapm_mixer_named_ctl this is simply the
402 * kcontrol name.
403 */
404 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000405 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000406 name_len += 1 + strlen(w->name);
407
Mark Brown219b93f2008-10-28 13:02:31 +0000408 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000409
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 if (path->long_name == NULL)
411 return -ENOMEM;
412
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000413 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000414 default:
415 snprintf(path->long_name, name_len, "%s %s",
416 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000417 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000418 case snd_soc_dapm_mixer_named_ctl:
419 snprintf(path->long_name, name_len, "%s",
420 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000421 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000422 }
423
Mark Brown219b93f2008-10-28 13:02:31 +0000424 path->long_name[name_len - 1] = '\0';
425
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
427 path->long_name);
428 ret = snd_ctl_add(codec->card, path->kcontrol);
429 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100430 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
431 path->long_name,
432 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433 kfree(path->long_name);
434 path->long_name = NULL;
435 return ret;
436 }
437 }
438 }
439 return ret;
440}
441
442/* create new dapm mux control */
443static int dapm_new_mux(struct snd_soc_codec *codec,
444 struct snd_soc_dapm_widget *w)
445{
446 struct snd_soc_dapm_path *path = NULL;
447 struct snd_kcontrol *kcontrol;
448 int ret = 0;
449
450 if (!w->num_kcontrols) {
451 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
452 return -EINVAL;
453 }
454
455 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
456 ret = snd_ctl_add(codec->card, kcontrol);
457 if (ret < 0)
458 goto err;
459
460 list_for_each_entry(path, &w->sources, list_sink)
461 path->kcontrol = kcontrol;
462
463 return ret;
464
465err:
466 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
467 return ret;
468}
469
470/* create new dapm volume control */
471static int dapm_new_pga(struct snd_soc_codec *codec,
472 struct snd_soc_dapm_widget *w)
473{
474 struct snd_kcontrol *kcontrol;
475 int ret = 0;
476
477 if (!w->num_kcontrols)
478 return -EINVAL;
479
480 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
481 ret = snd_ctl_add(codec->card, kcontrol);
482 if (ret < 0) {
483 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
484 return ret;
485 }
486
487 return ret;
488}
489
490/* reset 'walked' bit for each dapm path */
491static inline void dapm_clear_walk(struct snd_soc_codec *codec)
492{
493 struct snd_soc_dapm_path *p;
494
495 list_for_each_entry(p, &codec->dapm_paths, list)
496 p->walked = 0;
497}
498
499/*
500 * Recursively check for a completed path to an active or physically connected
501 * output widget. Returns number of complete paths.
502 */
503static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
504{
505 struct snd_soc_dapm_path *path;
506 int con = 0;
507
Mark Brown246d0a12009-04-22 18:24:55 +0100508 if (widget->id == snd_soc_dapm_supply)
509 return 0;
510
Mark Brown010ff262009-08-17 17:39:22 +0100511 switch (widget->id) {
512 case snd_soc_dapm_adc:
513 case snd_soc_dapm_aif_out:
514 if (widget->active)
515 return 1;
516 default:
517 break;
518 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200519
520 if (widget->connected) {
521 /* connected pin ? */
522 if (widget->id == snd_soc_dapm_output && !widget->ext)
523 return 1;
524
525 /* connected jack or spk ? */
526 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
527 widget->id == snd_soc_dapm_line)
528 return 1;
529 }
530
531 list_for_each_entry(path, &widget->sinks, list_source) {
532 if (path->walked)
533 continue;
534
535 if (path->sink && path->connect) {
536 path->walked = 1;
537 con += is_connected_output_ep(path->sink);
538 }
539 }
540
541 return con;
542}
543
544/*
545 * Recursively check for a completed path to an active or physically connected
546 * input widget. Returns number of complete paths.
547 */
548static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
549{
550 struct snd_soc_dapm_path *path;
551 int con = 0;
552
Mark Brown246d0a12009-04-22 18:24:55 +0100553 if (widget->id == snd_soc_dapm_supply)
554 return 0;
555
Richard Purdie2b97eab2006-10-06 18:32:18 +0200556 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100557 switch (widget->id) {
558 case snd_soc_dapm_dac:
559 case snd_soc_dapm_aif_in:
560 if (widget->active)
561 return 1;
562 default:
563 break;
564 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565
566 if (widget->connected) {
567 /* connected pin ? */
568 if (widget->id == snd_soc_dapm_input && !widget->ext)
569 return 1;
570
571 /* connected VMID/Bias for lower pops */
572 if (widget->id == snd_soc_dapm_vmid)
573 return 1;
574
575 /* connected jack ? */
576 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
577 return 1;
578 }
579
580 list_for_each_entry(path, &widget->sources, list_sink) {
581 if (path->walked)
582 continue;
583
584 if (path->source && path->connect) {
585 path->walked = 1;
586 con += is_connected_input_ep(path->source);
587 }
588 }
589
590 return con;
591}
592
593/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300594 * Handler for generic register modifier widget.
595 */
596int dapm_reg_event(struct snd_soc_dapm_widget *w,
597 struct snd_kcontrol *kcontrol, int event)
598{
599 unsigned int val;
600
601 if (SND_SOC_DAPM_EVENT_ON(event))
602 val = w->on_val;
603 else
604 val = w->off_val;
605
606 snd_soc_update_bits(w->codec, -(w->reg + 1),
607 w->mask << w->shift, val << w->shift);
608
609 return 0;
610}
Mark Brown11589412008-07-29 11:42:23 +0100611EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300612
Mark Brown025756e2009-04-13 11:09:18 +0100613/* Standard power change method, used to apply power changes to most
614 * widgets.
615 */
616static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
617{
618 int ret;
619
620 /* call any power change event handlers */
621 if (w->event)
622 pr_debug("power %s event for %s flags %x\n",
623 w->power ? "on" : "off",
624 w->name, w->event_flags);
625
626 /* power up pre event */
627 if (w->power && w->event &&
628 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
629 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
630 if (ret < 0)
631 return ret;
632 }
633
634 /* power down pre event */
635 if (!w->power && w->event &&
636 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
637 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
638 if (ret < 0)
639 return ret;
640 }
641
642 /* Lower PGA volume to reduce pops */
643 if (w->id == snd_soc_dapm_pga && !w->power)
644 dapm_set_pga(w, w->power);
645
646 dapm_update_bits(w);
647
648 /* Raise PGA volume to reduce pops */
649 if (w->id == snd_soc_dapm_pga && w->power)
650 dapm_set_pga(w, w->power);
651
652 /* power up post event */
653 if (w->power && w->event &&
654 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
655 ret = w->event(w,
656 NULL, SND_SOC_DAPM_POST_PMU);
657 if (ret < 0)
658 return ret;
659 }
660
661 /* power down post event */
662 if (!w->power && w->event &&
663 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
664 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
665 if (ret < 0)
666 return ret;
667 }
668
669 return 0;
670}
671
Mark Browncd0f2d42009-04-20 16:56:59 +0100672/* Generic check to see if a widget should be powered.
673 */
674static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
675{
676 int in, out;
677
678 in = is_connected_input_ep(w);
679 dapm_clear_walk(w->codec);
680 out = is_connected_output_ep(w);
681 dapm_clear_walk(w->codec);
682 return out != 0 && in != 0;
683}
684
Mark Brown6ea31b92009-04-20 17:15:41 +0100685/* Check to see if an ADC has power */
686static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
687{
688 int in;
689
690 if (w->active) {
691 in = is_connected_input_ep(w);
692 dapm_clear_walk(w->codec);
693 return in != 0;
694 } else {
695 return dapm_generic_check_power(w);
696 }
697}
698
699/* Check to see if a DAC has power */
700static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
701{
702 int out;
703
704 if (w->active) {
705 out = is_connected_output_ep(w);
706 dapm_clear_walk(w->codec);
707 return out != 0;
708 } else {
709 return dapm_generic_check_power(w);
710 }
711}
712
Mark Brown246d0a12009-04-22 18:24:55 +0100713/* Check to see if a power supply is needed */
714static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
715{
716 struct snd_soc_dapm_path *path;
717 int power = 0;
718
719 /* Check if one of our outputs is connected */
720 list_for_each_entry(path, &w->sinks, list_source) {
721 if (path->sink && path->sink->power_check &&
722 path->sink->power_check(path->sink)) {
723 power = 1;
724 break;
725 }
726 }
727
728 dapm_clear_walk(w->codec);
729
730 return power;
731}
732
Mark Brown38357ab2009-06-06 19:03:23 +0100733static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
734 struct snd_soc_dapm_widget *b,
735 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000736{
Mark Brown38357ab2009-06-06 19:03:23 +0100737 if (sort[a->id] != sort[b->id])
738 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100739 if (a->reg != b->reg)
740 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000741
Mark Brown38357ab2009-06-06 19:03:23 +0100742 return 0;
743}
Mark Brown42aa3412009-03-01 19:21:10 +0000744
Mark Brown38357ab2009-06-06 19:03:23 +0100745/* Insert a widget in order into a DAPM power sequence. */
746static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
747 struct list_head *list,
748 int sort[])
749{
750 struct snd_soc_dapm_widget *w;
751
752 list_for_each_entry(w, list, power_list)
753 if (dapm_seq_compare(new_widget, w, sort) < 0) {
754 list_add_tail(&new_widget->power_list, &w->power_list);
755 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000756 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100757
Mark Brown38357ab2009-06-06 19:03:23 +0100758 list_add_tail(&new_widget->power_list, list);
759}
Mark Brown42aa3412009-03-01 19:21:10 +0000760
Mark Brownb22ead22009-06-07 12:51:26 +0100761/* Apply the coalesced changes from a DAPM sequence */
762static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
763 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100764{
765 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100766 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100767 unsigned int value = 0;
768 unsigned int mask = 0;
769 unsigned int cur_mask;
770
771 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
772 power_list)->reg;
773
774 list_for_each_entry(w, pending, power_list) {
775 cur_mask = 1 << w->shift;
776 BUG_ON(reg != w->reg);
777
778 if (w->invert)
779 power = !w->power;
780 else
781 power = w->power;
782
783 mask |= cur_mask;
784 if (power)
785 value |= cur_mask;
786
787 pop_dbg(codec->pop_time,
788 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
789 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100790
791 /* power up pre event */
792 if (w->power && w->event &&
793 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
794 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
795 w->name);
796 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
797 if (ret < 0)
798 pr_err("%s: pre event failed: %d\n",
799 w->name, ret);
800 }
801
802 /* power down pre event */
803 if (!w->power && w->event &&
804 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
805 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
806 w->name);
807 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
808 if (ret < 0)
809 pr_err("%s: pre event failed: %d\n",
810 w->name, ret);
811 }
Mark Brown4f1c1922009-06-07 13:37:44 +0100812
813 /* Lower PGA volume to reduce pops */
814 if (w->id == snd_soc_dapm_pga && !w->power)
815 dapm_set_pga(w, w->power);
Mark Brownb22ead22009-06-07 12:51:26 +0100816 }
817
Mark Brown81628102009-06-07 13:21:24 +0100818 if (reg >= 0) {
819 pop_dbg(codec->pop_time,
820 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
821 value, mask, reg, codec->pop_time);
822 pop_wait(codec->pop_time);
823 snd_soc_update_bits(codec, reg, mask, value);
824 }
825
826 list_for_each_entry(w, pending, power_list) {
Mark Brown4f1c1922009-06-07 13:37:44 +0100827 /* Raise PGA volume to reduce pops */
828 if (w->id == snd_soc_dapm_pga && w->power)
829 dapm_set_pga(w, w->power);
830
Mark Brown81628102009-06-07 13:21:24 +0100831 /* power up post event */
832 if (w->power && w->event &&
833 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
834 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
835 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000836 ret = w->event(w,
837 NULL, SND_SOC_DAPM_POST_PMU);
838 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100839 pr_err("%s: post event failed: %d\n",
840 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000841 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100842
Mark Brown81628102009-06-07 13:21:24 +0100843 /* power down post event */
844 if (!w->power && w->event &&
845 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
846 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
847 w->name);
848 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
849 if (ret < 0)
850 pr_err("%s: post event failed: %d\n",
851 w->name, ret);
852 }
Mark Brown42aa3412009-03-01 19:21:10 +0000853 }
Mark Brown42aa3412009-03-01 19:21:10 +0000854}
855
Mark Brownb22ead22009-06-07 12:51:26 +0100856/* Apply a DAPM power sequence.
857 *
858 * We walk over a pre-sorted list of widgets to apply power to. In
859 * order to minimise the number of writes to the device required
860 * multiple widgets will be updated in a single write where possible.
861 * Currently anything that requires more than a single write is not
862 * handled.
863 */
864static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
865 int event, int sort[])
866{
867 struct snd_soc_dapm_widget *w, *n;
868 LIST_HEAD(pending);
869 int cur_sort = -1;
870 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100871 int ret;
872
Mark Brownb22ead22009-06-07 12:51:26 +0100873 list_for_each_entry_safe(w, n, list, power_list) {
874 ret = 0;
875
876 /* Do we need to apply any queued changes? */
877 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
878 if (!list_empty(&pending))
879 dapm_seq_run_coalesced(codec, &pending);
880
881 INIT_LIST_HEAD(&pending);
882 cur_sort = -1;
883 cur_reg = SND_SOC_NOPM;
884 }
885
Mark Brown163cac02009-06-07 10:12:52 +0100886 switch (w->id) {
887 case snd_soc_dapm_pre:
888 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100889 list_for_each_entry_safe_continue(w, n, list,
890 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100891
Mark Brownb22ead22009-06-07 12:51:26 +0100892 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100893 ret = w->event(w,
894 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100895 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100896 ret = w->event(w,
897 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100898 break;
899
900 case snd_soc_dapm_post:
901 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100902 list_for_each_entry_safe_continue(w, n, list,
903 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100904
Mark Brownb22ead22009-06-07 12:51:26 +0100905 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100906 ret = w->event(w,
907 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100908 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100909 ret = w->event(w,
910 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100911 break;
912
913 case snd_soc_dapm_input:
914 case snd_soc_dapm_output:
915 case snd_soc_dapm_hp:
916 case snd_soc_dapm_mic:
917 case snd_soc_dapm_line:
918 case snd_soc_dapm_spk:
919 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100920 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100921 break;
922
923 default:
Mark Brown81628102009-06-07 13:21:24 +0100924 /* Queue it up for application */
925 cur_sort = sort[w->id];
926 cur_reg = w->reg;
927 list_move(&w->power_list, &pending);
928 break;
Mark Brown163cac02009-06-07 10:12:52 +0100929 }
Mark Brownb22ead22009-06-07 12:51:26 +0100930
931 if (ret < 0)
932 pr_err("Failed to apply widget power: %d\n",
933 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100934 }
Mark Brownb22ead22009-06-07 12:51:26 +0100935
936 if (!list_empty(&pending))
937 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100938}
939
Mark Brown42aa3412009-03-01 19:21:10 +0000940/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 * Scan each dapm widget for complete audio path.
942 * A complete path is a route that has valid endpoints i.e.:-
943 *
944 * o DAC to output pin.
945 * o Input Pin to ADC.
946 * o Input pin to Output pin (bypass, sidetone)
947 * o DAC to ADC (loopback).
948 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100949static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950{
Mark Brown452c5ea2009-05-17 21:41:23 +0100951 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200952 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100953 LIST_HEAD(up_list);
954 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100955 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100956 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100957 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958
Mark Brown6d3ddc82009-05-16 17:47:29 +0100959 /* Check which widgets we need to power and store them in
960 * lists indicating if they should be powered up or down.
961 */
962 list_for_each_entry(w, &codec->dapm_widgets, list) {
963 switch (w->id) {
964 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100965 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100966 break;
967 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100968 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100969 break;
970
971 default:
972 if (!w->power_check)
973 continue;
974
975 power = w->power_check(w);
Mark Brown452c5ea2009-05-17 21:41:23 +0100976 if (power)
977 sys_power = 1;
978
Mark Brown6d3ddc82009-05-16 17:47:29 +0100979 if (w->power == power)
980 continue;
981
982 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100983 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100984 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100985 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100986
987 w->power = power;
988 break;
989 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990 }
991
Mark Brownb14b76a2009-08-17 11:55:38 +0100992 /* If there are no DAPM widgets then try to figure out power from the
993 * event type.
994 */
995 if (list_empty(&codec->dapm_widgets)) {
996 switch (event) {
997 case SND_SOC_DAPM_STREAM_START:
998 case SND_SOC_DAPM_STREAM_RESUME:
999 sys_power = 1;
1000 break;
1001 case SND_SOC_DAPM_STREAM_NOP:
1002 sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
1003 default:
1004 break;
1005 }
1006 }
1007
Mark Brown452c5ea2009-05-17 21:41:23 +01001008 /* If we're changing to all on or all off then prepare */
1009 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
1010 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
1011 ret = snd_soc_dapm_set_bias_level(socdev,
1012 SND_SOC_BIAS_PREPARE);
1013 if (ret != 0)
1014 pr_err("Failed to prepare bias: %d\n", ret);
1015 }
1016
Mark Brown6d3ddc82009-05-16 17:47:29 +01001017 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001018 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001019
1020 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001021 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022
Mark Brown452c5ea2009-05-17 21:41:23 +01001023 /* If we just powered the last thing off drop to standby bias */
1024 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1025 ret = snd_soc_dapm_set_bias_level(socdev,
1026 SND_SOC_BIAS_STANDBY);
1027 if (ret != 0)
1028 pr_err("Failed to apply standby bias: %d\n", ret);
1029 }
1030
1031 /* If we just powered up then move to active bias */
1032 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1033 ret = snd_soc_dapm_set_bias_level(socdev,
1034 SND_SOC_BIAS_ON);
1035 if (ret != 0)
1036 pr_err("Failed to apply active bias: %d\n", ret);
1037 }
1038
Mark Browncb507e72009-07-08 18:54:57 +01001039 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1040 codec->pop_time);
1041
Mark Brown42aa3412009-03-01 19:21:10 +00001042 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001043}
1044
Mark Brownc1286b82008-07-07 19:26:03 +01001045#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +02001046static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
1047{
1048 struct snd_soc_dapm_widget *w;
1049 struct snd_soc_dapm_path *p = NULL;
1050 int in, out;
1051
1052 printk("DAPM %s %s\n", codec->name, action);
1053
1054 list_for_each_entry(w, &codec->dapm_widgets, list) {
1055
1056 /* only display widgets that effect routing */
1057 switch (w->id) {
1058 case snd_soc_dapm_pre:
1059 case snd_soc_dapm_post:
1060 case snd_soc_dapm_vmid:
1061 continue;
1062 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001063 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001064 case snd_soc_dapm_output:
1065 case snd_soc_dapm_input:
1066 case snd_soc_dapm_switch:
1067 case snd_soc_dapm_hp:
1068 case snd_soc_dapm_mic:
1069 case snd_soc_dapm_spk:
1070 case snd_soc_dapm_line:
1071 case snd_soc_dapm_micbias:
1072 case snd_soc_dapm_dac:
1073 case snd_soc_dapm_adc:
1074 case snd_soc_dapm_pga:
1075 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001076 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001077 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001078 case snd_soc_dapm_aif_in:
1079 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001080 if (w->name) {
1081 in = is_connected_input_ep(w);
1082 dapm_clear_walk(w->codec);
1083 out = is_connected_output_ep(w);
1084 dapm_clear_walk(w->codec);
1085 printk("%s: %s in %d out %d\n", w->name,
1086 w->power ? "On":"Off",in, out);
1087
1088 list_for_each_entry(p, &w->sources, list_sink) {
1089 if (p->connect)
1090 printk(" in %s %s\n", p->name ? p->name : "static",
1091 p->source->name);
1092 }
1093 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001094 if (p->connect)
1095 printk(" out %s %s\n", p->name ? p->name : "static",
1096 p->sink->name);
1097 }
1098 }
1099 break;
1100 }
1101 }
1102}
1103#endif
1104
Mark Brown79fb9382009-08-21 16:38:13 +01001105#ifdef CONFIG_DEBUG_FS
1106static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1107{
1108 file->private_data = inode->i_private;
1109 return 0;
1110}
1111
1112static ssize_t dapm_widget_power_read_file(struct file *file,
1113 char __user *user_buf,
1114 size_t count, loff_t *ppos)
1115{
1116 struct snd_soc_dapm_widget *w = file->private_data;
1117 char *buf;
1118 int in, out;
1119 ssize_t ret;
1120 struct snd_soc_dapm_path *p = NULL;
1121
1122 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1123 if (!buf)
1124 return -ENOMEM;
1125
1126 in = is_connected_input_ep(w);
1127 dapm_clear_walk(w->codec);
1128 out = is_connected_output_ep(w);
1129 dapm_clear_walk(w->codec);
1130
1131 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d\n",
1132 w->name, w->power ? "On" : "Off", in, out);
1133
1134 if (w->active && w->sname)
1135 ret += snprintf(buf, PAGE_SIZE - ret, " stream %s active\n",
1136 w->sname);
1137
1138 list_for_each_entry(p, &w->sources, list_sink) {
1139 if (p->connect)
1140 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1141 " in %s %s\n",
1142 p->name ? p->name : "static",
1143 p->source->name);
1144 }
1145 list_for_each_entry(p, &w->sinks, list_source) {
1146 if (p->connect)
1147 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1148 " out %s %s\n",
1149 p->name ? p->name : "static",
1150 p->sink->name);
1151 }
1152
1153 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1154
1155 kfree(buf);
1156 return ret;
1157}
1158
1159static const struct file_operations dapm_widget_power_fops = {
1160 .open = dapm_widget_power_open_file,
1161 .read = dapm_widget_power_read_file,
1162};
1163
1164void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1165{
1166 struct snd_soc_dapm_widget *w;
1167 struct dentry *d;
1168
1169 if (!codec->debugfs_dapm)
1170 return;
1171
1172 list_for_each_entry(w, &codec->dapm_widgets, list) {
1173 if (!w->name)
1174 continue;
1175
1176 d = debugfs_create_file(w->name, 0444,
1177 codec->debugfs_dapm, w,
1178 &dapm_widget_power_fops);
1179 if (!d)
1180 printk(KERN_WARNING
1181 "ASoC: Failed to create %s debugfs file\n",
1182 w->name);
1183 }
1184}
1185#else
1186void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1187{
1188}
1189#endif
1190
Richard Purdie2b97eab2006-10-06 18:32:18 +02001191/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001192static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1193 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001194 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195{
1196 struct snd_soc_dapm_path *path;
1197 int found = 0;
1198
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001199 if (widget->id != snd_soc_dapm_mux &&
1200 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001201 return -ENODEV;
1202
1203 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
1204 return 0;
1205
1206 /* find dapm widget path assoc with kcontrol */
1207 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1208 if (path->kcontrol != kcontrol)
1209 continue;
1210
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001211 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001212 continue;
1213
1214 found = 1;
1215 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001216 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 path->connect = 1; /* new connection */
1218 else
1219 path->connect = 0; /* old connection must be powered down */
1220 }
1221
Mark Brown3fccd8b2008-07-07 19:26:04 +01001222 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001224 dump_dapm(widget->codec, "mux power update");
1225 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001226
1227 return 0;
1228}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229
Milan plzik1b075e32008-01-10 14:39:46 +01001230/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001231static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1232 struct snd_kcontrol *kcontrol, int reg,
1233 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001234{
1235 struct snd_soc_dapm_path *path;
1236 int found = 0;
1237
Milan plzik1b075e32008-01-10 14:39:46 +01001238 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001239 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001240 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001241 return -ENODEV;
1242
1243 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
1244 return 0;
1245
1246 /* find dapm widget path assoc with kcontrol */
1247 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1248 if (path->kcontrol != kcontrol)
1249 continue;
1250
1251 /* found, now check type */
1252 found = 1;
1253 if (val)
1254 /* new connection */
1255 path->connect = invert ? 0:1;
1256 else
1257 /* old connection must be powered down */
1258 path->connect = invert ? 1:0;
1259 break;
1260 }
1261
Mark Brown3fccd8b2008-07-07 19:26:04 +01001262 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001263 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001264 dump_dapm(widget->codec, "mixer power update");
1265 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266
1267 return 0;
1268}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001269
1270/* show dapm widget status in sys fs */
1271static ssize_t dapm_widget_show(struct device *dev,
1272 struct device_attribute *attr, char *buf)
1273{
1274 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001275 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001276 struct snd_soc_dapm_widget *w;
1277 int count = 0;
1278 char *state = "not set";
1279
1280 list_for_each_entry(w, &codec->dapm_widgets, list) {
1281
1282 /* only display widgets that burnm power */
1283 switch (w->id) {
1284 case snd_soc_dapm_hp:
1285 case snd_soc_dapm_mic:
1286 case snd_soc_dapm_spk:
1287 case snd_soc_dapm_line:
1288 case snd_soc_dapm_micbias:
1289 case snd_soc_dapm_dac:
1290 case snd_soc_dapm_adc:
1291 case snd_soc_dapm_pga:
1292 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001293 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001294 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001295 if (w->name)
1296 count += sprintf(buf + count, "%s: %s\n",
1297 w->name, w->power ? "On":"Off");
1298 break;
1299 default:
1300 break;
1301 }
1302 }
1303
Mark Brown0be98982008-05-19 12:31:28 +02001304 switch (codec->bias_level) {
1305 case SND_SOC_BIAS_ON:
1306 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001307 break;
Mark Brown0be98982008-05-19 12:31:28 +02001308 case SND_SOC_BIAS_PREPARE:
1309 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001310 break;
Mark Brown0be98982008-05-19 12:31:28 +02001311 case SND_SOC_BIAS_STANDBY:
1312 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313 break;
Mark Brown0be98982008-05-19 12:31:28 +02001314 case SND_SOC_BIAS_OFF:
1315 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316 break;
1317 }
1318 count += sprintf(buf + count, "PM State: %s\n", state);
1319
1320 return count;
1321}
1322
1323static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1324
1325int snd_soc_dapm_sys_add(struct device *dev)
1326{
Troy Kisky12ef1932008-10-13 17:42:14 -07001327 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001328}
1329
1330static void snd_soc_dapm_sys_remove(struct device *dev)
1331{
Mark Brownaef90842009-05-16 17:53:16 +01001332 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333}
1334
1335/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001336static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001337{
1338 struct snd_soc_dapm_widget *w, *next_w;
1339 struct snd_soc_dapm_path *p, *next_p;
1340
1341 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1342 list_del(&w->list);
1343 kfree(w);
1344 }
1345
1346 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1347 list_del(&p->list);
1348 kfree(p->long_name);
1349 kfree(p);
1350 }
1351}
1352
Liam Girdwooda5302182008-07-07 13:35:17 +01001353static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001354 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001355{
1356 struct snd_soc_dapm_widget *w;
1357
1358 list_for_each_entry(w, &codec->dapm_widgets, list) {
1359 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001360 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001361 w->connected = status;
1362 return 0;
1363 }
1364 }
1365
Mark Brownc1286b82008-07-07 19:26:03 +01001366 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001367 return -EINVAL;
1368}
1369
Richard Purdie2b97eab2006-10-06 18:32:18 +02001370/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001371 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001372 * @codec: audio codec
1373 *
1374 * Walks all dapm audio paths and powers widgets according to their
1375 * stream or path usage.
1376 *
1377 * Returns 0 for success.
1378 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001379int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001381 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1382 dump_dapm(codec, "sync");
1383 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001384}
Liam Girdwooda5302182008-07-07 13:35:17 +01001385EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001386
Mark Brown105f1c22008-05-13 14:52:19 +02001387static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1388 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001389{
1390 struct snd_soc_dapm_path *path;
1391 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1392 int ret = 0;
1393
1394 /* find src and dest widgets */
1395 list_for_each_entry(w, &codec->dapm_widgets, list) {
1396
1397 if (!wsink && !(strcmp(w->name, sink))) {
1398 wsink = w;
1399 continue;
1400 }
1401 if (!wsource && !(strcmp(w->name, source))) {
1402 wsource = w;
1403 }
1404 }
1405
1406 if (wsource == NULL || wsink == NULL)
1407 return -ENODEV;
1408
1409 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1410 if (!path)
1411 return -ENOMEM;
1412
1413 path->source = wsource;
1414 path->sink = wsink;
1415 INIT_LIST_HEAD(&path->list);
1416 INIT_LIST_HEAD(&path->list_source);
1417 INIT_LIST_HEAD(&path->list_sink);
1418
1419 /* check for external widgets */
1420 if (wsink->id == snd_soc_dapm_input) {
1421 if (wsource->id == snd_soc_dapm_micbias ||
1422 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001423 wsource->id == snd_soc_dapm_line ||
1424 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001425 wsink->ext = 1;
1426 }
1427 if (wsource->id == snd_soc_dapm_output) {
1428 if (wsink->id == snd_soc_dapm_spk ||
1429 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001430 wsink->id == snd_soc_dapm_line ||
1431 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001432 wsource->ext = 1;
1433 }
1434
1435 /* connect static paths */
1436 if (control == NULL) {
1437 list_add(&path->list, &codec->dapm_paths);
1438 list_add(&path->list_sink, &wsink->sources);
1439 list_add(&path->list_source, &wsource->sinks);
1440 path->connect = 1;
1441 return 0;
1442 }
1443
1444 /* connect dynamic paths */
1445 switch(wsink->id) {
1446 case snd_soc_dapm_adc:
1447 case snd_soc_dapm_dac:
1448 case snd_soc_dapm_pga:
1449 case snd_soc_dapm_input:
1450 case snd_soc_dapm_output:
1451 case snd_soc_dapm_micbias:
1452 case snd_soc_dapm_vmid:
1453 case snd_soc_dapm_pre:
1454 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001455 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001456 case snd_soc_dapm_aif_in:
1457 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001458 list_add(&path->list, &codec->dapm_paths);
1459 list_add(&path->list_sink, &wsink->sources);
1460 list_add(&path->list_source, &wsource->sinks);
1461 path->connect = 1;
1462 return 0;
1463 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001464 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001465 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1466 &wsink->kcontrols[0]);
1467 if (ret != 0)
1468 goto err;
1469 break;
1470 case snd_soc_dapm_switch:
1471 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001472 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1474 if (ret != 0)
1475 goto err;
1476 break;
1477 case snd_soc_dapm_hp:
1478 case snd_soc_dapm_mic:
1479 case snd_soc_dapm_line:
1480 case snd_soc_dapm_spk:
1481 list_add(&path->list, &codec->dapm_paths);
1482 list_add(&path->list_sink, &wsink->sources);
1483 list_add(&path->list_source, &wsource->sinks);
1484 path->connect = 0;
1485 return 0;
1486 }
1487 return 0;
1488
1489err:
1490 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1491 control, sink);
1492 kfree(path);
1493 return ret;
1494}
Mark Brown105f1c22008-05-13 14:52:19 +02001495
1496/**
Mark Brown105f1c22008-05-13 14:52:19 +02001497 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1498 * @codec: codec
1499 * @route: audio routes
1500 * @num: number of routes
1501 *
1502 * Connects 2 dapm widgets together via a named audio path. The sink is
1503 * the widget receiving the audio signal, whilst the source is the sender
1504 * of the audio signal.
1505 *
1506 * Returns 0 for success else error. On error all resources can be freed
1507 * with a call to snd_soc_card_free().
1508 */
1509int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1510 const struct snd_soc_dapm_route *route, int num)
1511{
1512 int i, ret;
1513
1514 for (i = 0; i < num; i++) {
1515 ret = snd_soc_dapm_add_route(codec, route->sink,
1516 route->control, route->source);
1517 if (ret < 0) {
1518 printk(KERN_ERR "Failed to add route %s->%s\n",
1519 route->source,
1520 route->sink);
1521 return ret;
1522 }
1523 route++;
1524 }
1525
1526 return 0;
1527}
1528EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1529
1530/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531 * snd_soc_dapm_new_widgets - add new dapm widgets
1532 * @codec: audio codec
1533 *
1534 * Checks the codec for any new dapm widgets and creates them if found.
1535 *
1536 * Returns 0 for success.
1537 */
1538int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1539{
1540 struct snd_soc_dapm_widget *w;
1541
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 list_for_each_entry(w, &codec->dapm_widgets, list)
1543 {
1544 if (w->new)
1545 continue;
1546
1547 switch(w->id) {
1548 case snd_soc_dapm_switch:
1549 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001550 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001551 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001552 dapm_new_mixer(codec, w);
1553 break;
1554 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001555 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001556 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557 dapm_new_mux(codec, w);
1558 break;
1559 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001560 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001561 w->power_check = dapm_adc_check_power;
1562 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001563 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001564 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001565 w->power_check = dapm_dac_check_power;
1566 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001568 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001569 dapm_new_pga(codec, w);
1570 break;
1571 case snd_soc_dapm_input:
1572 case snd_soc_dapm_output:
1573 case snd_soc_dapm_micbias:
1574 case snd_soc_dapm_spk:
1575 case snd_soc_dapm_hp:
1576 case snd_soc_dapm_mic:
1577 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001578 w->power_check = dapm_generic_check_power;
1579 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001580 case snd_soc_dapm_supply:
1581 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001582 case snd_soc_dapm_vmid:
1583 case snd_soc_dapm_pre:
1584 case snd_soc_dapm_post:
1585 break;
1586 }
1587 w->new = 1;
1588 }
1589
1590 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001591 return 0;
1592}
1593EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1594
1595/**
1596 * snd_soc_dapm_get_volsw - dapm mixer get callback
1597 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001598 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001599 *
1600 * Callback to get the value of a dapm mixer control.
1601 *
1602 * Returns 0 for success.
1603 */
1604int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
1606{
1607 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001608 struct soc_mixer_control *mc =
1609 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001610 unsigned int reg = mc->reg;
1611 unsigned int shift = mc->shift;
1612 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001613 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001614 unsigned int invert = mc->invert;
1615 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001616
1617 /* return the saved value if we are powered down */
1618 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1619 ucontrol->value.integer.value[0] = widget->saved_value;
1620 return 0;
1621 }
1622
1623 ucontrol->value.integer.value[0] =
1624 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1625 if (shift != rshift)
1626 ucontrol->value.integer.value[1] =
1627 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1628 if (invert) {
1629 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001630 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 if (shift != rshift)
1632 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001633 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634 }
1635
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1639
1640/**
1641 * snd_soc_dapm_put_volsw - dapm mixer set callback
1642 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001643 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 *
1645 * Callback to set the value of a dapm mixer control.
1646 *
1647 * Returns 0 for success.
1648 */
1649int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
1651{
1652 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001653 struct soc_mixer_control *mc =
1654 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001655 unsigned int reg = mc->reg;
1656 unsigned int shift = mc->shift;
1657 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001658 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001659 unsigned int mask = (1 << fls(max)) - 1;
1660 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001661 unsigned int val, val2, val_mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 int ret;
1663
1664 val = (ucontrol->value.integer.value[0] & mask);
1665
1666 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001667 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 val_mask = mask << shift;
1669 val = val << shift;
1670 if (shift != rshift) {
1671 val2 = (ucontrol->value.integer.value[1] & mask);
1672 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001673 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 val_mask |= mask << rshift;
1675 val |= val2 << rshift;
1676 }
1677
1678 mutex_lock(&widget->codec->mutex);
1679 widget->value = val;
1680
1681 /* save volume value if the widget is powered down */
1682 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1683 widget->saved_value = val;
1684 mutex_unlock(&widget->codec->mutex);
1685 return 1;
1686 }
1687
1688 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1689 if (widget->event) {
1690 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001691 ret = widget->event(widget, kcontrol,
1692 SND_SOC_DAPM_PRE_REG);
1693 if (ret < 0) {
1694 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001696 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001697 }
1698 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1699 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001700 ret = widget->event(widget, kcontrol,
1701 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001702 } else
1703 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1704
1705out:
1706 mutex_unlock(&widget->codec->mutex);
1707 return ret;
1708}
1709EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1710
1711/**
1712 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1713 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001714 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 *
1716 * Callback to get the value of a dapm enumerated double mixer control.
1717 *
1718 * Returns 0 for success.
1719 */
1720int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1724 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001725 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001727 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728 ;
1729 val = snd_soc_read(widget->codec, e->reg);
1730 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1731 if (e->shift_l != e->shift_r)
1732 ucontrol->value.enumerated.item[1] =
1733 (val >> e->shift_r) & (bitmask - 1);
1734
1735 return 0;
1736}
1737EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1738
1739/**
1740 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1741 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001742 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 *
1744 * Callback to set the value of a dapm enumerated double mixer control.
1745 *
1746 * Returns 0 for success.
1747 */
1748int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *ucontrol)
1750{
1751 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1752 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001753 unsigned int val, mux;
1754 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001755 int ret = 0;
1756
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001757 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001758 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001759 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760 return -EINVAL;
1761 mux = ucontrol->value.enumerated.item[0];
1762 val = mux << e->shift_l;
1763 mask = (bitmask - 1) << e->shift_l;
1764 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001765 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 return -EINVAL;
1767 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1768 mask |= (bitmask - 1) << e->shift_r;
1769 }
1770
1771 mutex_lock(&widget->codec->mutex);
1772 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001773 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 if (widget->event) {
1775 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001776 ret = widget->event(widget,
1777 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001778 if (ret < 0)
1779 goto out;
1780 }
1781 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1782 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001783 ret = widget->event(widget,
1784 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001785 } else
1786 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1787
1788out:
1789 mutex_unlock(&widget->codec->mutex);
1790 return ret;
1791}
1792EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1793
1794/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001795 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1796 * callback
1797 * @kcontrol: mixer control
1798 * @ucontrol: control element information
1799 *
1800 * Callback to get the value of a dapm semi enumerated double mixer control.
1801 *
1802 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1803 * used for handling bitfield coded enumeration for example.
1804 *
1805 * Returns 0 for success.
1806 */
1807int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1808 struct snd_ctl_elem_value *ucontrol)
1809{
1810 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001811 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001812 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001813
1814 reg_val = snd_soc_read(widget->codec, e->reg);
1815 val = (reg_val >> e->shift_l) & e->mask;
1816 for (mux = 0; mux < e->max; mux++) {
1817 if (val == e->values[mux])
1818 break;
1819 }
1820 ucontrol->value.enumerated.item[0] = mux;
1821 if (e->shift_l != e->shift_r) {
1822 val = (reg_val >> e->shift_r) & e->mask;
1823 for (mux = 0; mux < e->max; mux++) {
1824 if (val == e->values[mux])
1825 break;
1826 }
1827 ucontrol->value.enumerated.item[1] = mux;
1828 }
1829
1830 return 0;
1831}
1832EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1833
1834/**
1835 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1836 * callback
1837 * @kcontrol: mixer control
1838 * @ucontrol: control element information
1839 *
1840 * Callback to set the value of a dapm semi enumerated double mixer control.
1841 *
1842 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1843 * used for handling bitfield coded enumeration for example.
1844 *
1845 * Returns 0 for success.
1846 */
1847int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_value *ucontrol)
1849{
1850 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001851 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001852 unsigned int val, mux;
1853 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001854 int ret = 0;
1855
1856 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1857 return -EINVAL;
1858 mux = ucontrol->value.enumerated.item[0];
1859 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1860 mask = e->mask << e->shift_l;
1861 if (e->shift_l != e->shift_r) {
1862 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1863 return -EINVAL;
1864 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1865 mask |= e->mask << e->shift_r;
1866 }
1867
1868 mutex_lock(&widget->codec->mutex);
1869 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001870 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001871 if (widget->event) {
1872 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1873 ret = widget->event(widget,
1874 kcontrol, SND_SOC_DAPM_PRE_REG);
1875 if (ret < 0)
1876 goto out;
1877 }
1878 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1879 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1880 ret = widget->event(widget,
1881 kcontrol, SND_SOC_DAPM_POST_REG);
1882 } else
1883 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1884
1885out:
1886 mutex_unlock(&widget->codec->mutex);
1887 return ret;
1888}
1889EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1890
1891/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001892 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1893 *
1894 * @kcontrol: mixer control
1895 * @uinfo: control element information
1896 *
1897 * Callback to provide information about a pin switch control.
1898 */
1899int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1900 struct snd_ctl_elem_info *uinfo)
1901{
1902 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1903 uinfo->count = 1;
1904 uinfo->value.integer.min = 0;
1905 uinfo->value.integer.max = 1;
1906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1910
1911/**
1912 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1913 *
1914 * @kcontrol: mixer control
1915 * @ucontrol: Value
1916 */
1917int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1919{
1920 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1921 const char *pin = (const char *)kcontrol->private_value;
1922
1923 mutex_lock(&codec->mutex);
1924
1925 ucontrol->value.integer.value[0] =
1926 snd_soc_dapm_get_pin_status(codec, pin);
1927
1928 mutex_unlock(&codec->mutex);
1929
1930 return 0;
1931}
1932EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1933
1934/**
1935 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1936 *
1937 * @kcontrol: mixer control
1938 * @ucontrol: Value
1939 */
1940int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1941 struct snd_ctl_elem_value *ucontrol)
1942{
1943 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1944 const char *pin = (const char *)kcontrol->private_value;
1945
1946 mutex_lock(&codec->mutex);
1947
1948 if (ucontrol->value.integer.value[0])
1949 snd_soc_dapm_enable_pin(codec, pin);
1950 else
1951 snd_soc_dapm_disable_pin(codec, pin);
1952
1953 snd_soc_dapm_sync(codec);
1954
1955 mutex_unlock(&codec->mutex);
1956
1957 return 0;
1958}
1959EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1960
1961/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 * snd_soc_dapm_new_control - create new dapm control
1963 * @codec: audio codec
1964 * @widget: widget template
1965 *
1966 * Creates a new dapm control based upon the template.
1967 *
1968 * Returns 0 for success else error.
1969 */
1970int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1971 const struct snd_soc_dapm_widget *widget)
1972{
1973 struct snd_soc_dapm_widget *w;
1974
1975 if ((w = dapm_cnew_widget(widget)) == NULL)
1976 return -ENOMEM;
1977
1978 w->codec = codec;
1979 INIT_LIST_HEAD(&w->sources);
1980 INIT_LIST_HEAD(&w->sinks);
1981 INIT_LIST_HEAD(&w->list);
1982 list_add(&w->list, &codec->dapm_widgets);
1983
1984 /* machine layer set ups unconnected pins and insertions */
1985 w->connected = 1;
1986 return 0;
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1989
1990/**
Mark Brown4ba13272008-05-13 14:51:19 +02001991 * snd_soc_dapm_new_controls - create new dapm controls
1992 * @codec: audio codec
1993 * @widget: widget array
1994 * @num: number of widgets
1995 *
1996 * Creates new DAPM controls based upon the templates.
1997 *
1998 * Returns 0 for success else error.
1999 */
2000int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
2001 const struct snd_soc_dapm_widget *widget,
2002 int num)
2003{
2004 int i, ret;
2005
2006 for (i = 0; i < num; i++) {
2007 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002008 if (ret < 0) {
2009 printk(KERN_ERR
2010 "ASoC: Failed to create DAPM control %s: %d\n",
2011 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002012 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002013 }
Mark Brown4ba13272008-05-13 14:51:19 +02002014 widget++;
2015 }
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2019
2020
2021/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2023 * @codec: audio codec
2024 * @stream: stream name
2025 * @event: stream event
2026 *
2027 * Sends a stream event to the dapm core. The core then makes any
2028 * necessary widget power changes.
2029 *
2030 * Returns 0 for success else error.
2031 */
2032int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2033 char *stream, int event)
2034{
2035 struct snd_soc_dapm_widget *w;
2036
Seth Forshee11da21a2007-02-02 17:14:19 +01002037 if (stream == NULL)
2038 return 0;
2039
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 mutex_lock(&codec->mutex);
2041 list_for_each_entry(w, &codec->dapm_widgets, list)
2042 {
2043 if (!w->sname)
2044 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002045 pr_debug("widget %s\n %s stream %s event %d\n",
2046 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 if (strstr(w->sname, stream)) {
2048 switch(event) {
2049 case SND_SOC_DAPM_STREAM_START:
2050 w->active = 1;
2051 break;
2052 case SND_SOC_DAPM_STREAM_STOP:
2053 w->active = 0;
2054 break;
2055 case SND_SOC_DAPM_STREAM_SUSPEND:
2056 if (w->active)
2057 w->suspend = 1;
2058 w->active = 0;
2059 break;
2060 case SND_SOC_DAPM_STREAM_RESUME:
2061 if (w->suspend) {
2062 w->active = 1;
2063 w->suspend = 0;
2064 }
2065 break;
2066 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2067 break;
2068 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2069 break;
2070 }
2071 }
2072 }
2073 mutex_unlock(&codec->mutex);
2074
2075 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08002076 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002077 return 0;
2078}
2079EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2080
2081/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002082 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002083 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002084 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 *
Mark Brown74b8f952009-06-06 11:26:15 +01002086 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002087 * a valid audio route and active audio stream.
2088 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2089 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 */
Mark Brown16499232009-01-07 18:25:13 +00002091int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092{
Liam Girdwooda5302182008-07-07 13:35:17 +01002093 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094}
Liam Girdwooda5302182008-07-07 13:35:17 +01002095EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096
2097/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002098 * snd_soc_dapm_disable_pin - disable pin.
2099 * @codec: SoC codec
2100 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002101 *
Mark Brown74b8f952009-06-06 11:26:15 +01002102 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002103 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2104 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002105 */
Mark Brown16499232009-01-07 18:25:13 +00002106int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002107{
2108 return snd_soc_dapm_set_pin(codec, pin, 0);
2109}
2110EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2111
2112/**
Mark Brown5817b522008-09-24 11:23:11 +01002113 * snd_soc_dapm_nc_pin - permanently disable pin.
2114 * @codec: SoC codec
2115 * @pin: pin name
2116 *
2117 * Marks the specified pin as being not connected, disabling it along
2118 * any parent or child widgets. At present this is identical to
2119 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2120 * additional things such as disabling controls which only affect
2121 * paths through the pin.
2122 *
2123 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2124 * do any widget power switching.
2125 */
Mark Brown16499232009-01-07 18:25:13 +00002126int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002127{
2128 return snd_soc_dapm_set_pin(codec, pin, 0);
2129}
2130EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2131
2132/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002133 * snd_soc_dapm_get_pin_status - get audio pin status
2134 * @codec: audio codec
2135 * @pin: audio signal pin endpoint (or start point)
2136 *
2137 * Get audio pin status - connected or disconnected.
2138 *
2139 * Returns 1 for connected otherwise 0.
2140 */
Mark Brown16499232009-01-07 18:25:13 +00002141int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002142{
2143 struct snd_soc_dapm_widget *w;
2144
2145 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002146 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002147 return w->connected;
2148 }
2149
2150 return 0;
2151}
Liam Girdwooda5302182008-07-07 13:35:17 +01002152EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002153
2154/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155 * snd_soc_dapm_free - free dapm resources
2156 * @socdev: SoC device
2157 *
2158 * Free all dapm widgets and resources.
2159 */
2160void snd_soc_dapm_free(struct snd_soc_device *socdev)
2161{
Mark Brown6627a652009-01-23 22:55:23 +00002162 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163
2164 snd_soc_dapm_sys_remove(socdev->dev);
2165 dapm_free_widgets(codec);
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2168
Mark Brown51737472009-06-22 13:16:51 +01002169/*
2170 * snd_soc_dapm_shutdown - callback for system shutdown
2171 */
2172void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2173{
2174 struct snd_soc_codec *codec = socdev->card->codec;
2175 struct snd_soc_dapm_widget *w;
2176 LIST_HEAD(down_list);
2177 int powerdown = 0;
2178
2179 list_for_each_entry(w, &codec->dapm_widgets, list) {
2180 if (w->power) {
2181 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002182 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002183 powerdown = 1;
2184 }
2185 }
2186
2187 /* If there were no widgets to power down we're already in
2188 * standby.
2189 */
2190 if (powerdown) {
2191 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2192 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2193 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2194 }
2195
2196 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2197}
2198
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002200MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2202MODULE_LICENSE("GPL");