blob: 2f5295dbd1586bff0a24926e709d454335f9501d [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>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc-dapm.h>
44#include <sound/initval.h>
45
46/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010047#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#else
50#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#endif
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010055 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
58 [snd_soc_dapm_mic] = 3,
59 [snd_soc_dapm_mux] = 4,
Mark Browne3d4dab2009-06-07 13:08:45 +010060 [snd_soc_dapm_value_mux] = 4,
61 [snd_soc_dapm_dac] = 5,
62 [snd_soc_dapm_mixer] = 6,
63 [snd_soc_dapm_mixer_named_ctl] = 6,
64 [snd_soc_dapm_pga] = 7,
65 [snd_soc_dapm_adc] = 8,
66 [snd_soc_dapm_hp] = 9,
Mark Brown1ca04062009-08-17 16:26:59 +010067 [snd_soc_dapm_spk] = 9,
68 [snd_soc_dapm_post] = 10,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000070
Richard Purdie2b97eab2006-10-06 18:32:18 +020071static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010075 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pga] = 4,
77 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010078 [snd_soc_dapm_mixer] = 5,
79 [snd_soc_dapm_dac] = 6,
80 [snd_soc_dapm_mic] = 7,
81 [snd_soc_dapm_micbias] = 8,
82 [snd_soc_dapm_mux] = 9,
83 [snd_soc_dapm_value_mux] = 9,
84 [snd_soc_dapm_supply] = 10,
85 [snd_soc_dapm_post] = 11,
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
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 va_list args;
97
98 va_start(args, fmt);
99
100 if (pop_time) {
101 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700102 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100103 }
104
105 va_end(args);
106}
107
Richard Purdie2b97eab2006-10-06 18:32:18 +0200108/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100109static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200110 const struct snd_soc_dapm_widget *_widget)
111{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100112 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200113}
114
Mark Brown452c5ea2009-05-17 21:41:23 +0100115/**
116 * snd_soc_dapm_set_bias_level - set the bias level for the system
117 * @socdev: audio device
118 * @level: level to configure
119 *
120 * Configure the bias (power) levels for the SoC audio device.
121 *
122 * Returns 0 for success else error.
123 */
124static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
125 enum snd_soc_bias_level level)
126{
127 struct snd_soc_card *card = socdev->card;
128 struct snd_soc_codec *codec = socdev->card->codec;
129 int ret = 0;
130
Mark Brownf83fba82009-05-18 15:44:43 +0100131 switch (level) {
132 case SND_SOC_BIAS_ON:
133 dev_dbg(socdev->dev, "Setting full bias\n");
134 break;
135 case SND_SOC_BIAS_PREPARE:
136 dev_dbg(socdev->dev, "Setting bias prepare\n");
137 break;
138 case SND_SOC_BIAS_STANDBY:
139 dev_dbg(socdev->dev, "Setting standby bias\n");
140 break;
141 case SND_SOC_BIAS_OFF:
142 dev_dbg(socdev->dev, "Setting bias off\n");
143 break;
144 default:
145 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
146 return -EINVAL;
147 }
148
Mark Brown452c5ea2009-05-17 21:41:23 +0100149 if (card->set_bias_level)
150 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100151 if (ret == 0) {
152 if (codec->set_bias_level)
153 ret = codec->set_bias_level(codec, level);
154 else
155 codec->bias_level = level;
156 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100157
158 return ret;
159}
160
Richard Purdie2b97eab2006-10-06 18:32:18 +0200161/* set up initial codec paths */
162static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
163 struct snd_soc_dapm_path *p, int i)
164{
165 switch (w->id) {
166 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000167 case snd_soc_dapm_mixer:
168 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200169 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100170 struct soc_mixer_control *mc = (struct soc_mixer_control *)
171 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400172 unsigned int reg = mc->reg;
173 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100174 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400175 unsigned int mask = (1 << fls(max)) - 1;
176 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200177
178 val = snd_soc_read(w->codec, reg);
179 val = (val >> shift) & mask;
180
181 if ((invert && !val) || (!invert && val))
182 p->connect = 1;
183 else
184 p->connect = 0;
185 }
186 break;
187 case snd_soc_dapm_mux: {
188 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
189 int val, item, bitmask;
190
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100191 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200192 ;
193 val = snd_soc_read(w->codec, e->reg);
194 item = (val >> e->shift_l) & (bitmask - 1);
195
196 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100197 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200198 if (!(strcmp(p->name, e->texts[i])) && item == i)
199 p->connect = 1;
200 }
201 }
202 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200203 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200204 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200205 w->kcontrols[i].private_value;
206 int val, item;
207
208 val = snd_soc_read(w->codec, e->reg);
209 val = (val >> e->shift_l) & e->mask;
210 for (item = 0; item < e->max; item++) {
211 if (val == e->values[item])
212 break;
213 }
214
215 p->connect = 0;
216 for (i = 0; i < e->max; i++) {
217 if (!(strcmp(p->name, e->texts[i])) && item == i)
218 p->connect = 1;
219 }
220 }
221 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200222 /* does not effect routing - always connected */
223 case snd_soc_dapm_pga:
224 case snd_soc_dapm_output:
225 case snd_soc_dapm_adc:
226 case snd_soc_dapm_input:
227 case snd_soc_dapm_dac:
228 case snd_soc_dapm_micbias:
229 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100230 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200231 p->connect = 1;
232 break;
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp:
235 case snd_soc_dapm_mic:
236 case snd_soc_dapm_spk:
237 case snd_soc_dapm_line:
238 case snd_soc_dapm_pre:
239 case snd_soc_dapm_post:
240 p->connect = 0;
241 break;
242 }
243}
244
Mark Brown74b8f952009-06-06 11:26:15 +0100245/* connect mux widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200246static int dapm_connect_mux(struct snd_soc_codec *codec,
247 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
248 struct snd_soc_dapm_path *path, const char *control_name,
249 const struct snd_kcontrol_new *kcontrol)
250{
251 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
252 int i;
253
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100254 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200255 if (!(strcmp(control_name, e->texts[i]))) {
256 list_add(&path->list, &codec->dapm_paths);
257 list_add(&path->list_sink, &dest->sources);
258 list_add(&path->list_source, &src->sinks);
259 path->name = (char*)e->texts[i];
260 dapm_set_path_status(dest, path, 0);
261 return 0;
262 }
263 }
264
265 return -ENODEV;
266}
267
Mark Brown74b8f952009-06-06 11:26:15 +0100268/* connect mixer widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200269static int dapm_connect_mixer(struct snd_soc_codec *codec,
270 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
271 struct snd_soc_dapm_path *path, const char *control_name)
272{
273 int i;
274
275 /* search for mixer kcontrol */
276 for (i = 0; i < dest->num_kcontrols; i++) {
277 if (!strcmp(control_name, dest->kcontrols[i].name)) {
278 list_add(&path->list, &codec->dapm_paths);
279 list_add(&path->list_sink, &dest->sources);
280 list_add(&path->list_source, &src->sinks);
281 path->name = dest->kcontrols[i].name;
282 dapm_set_path_status(dest, path, i);
283 return 0;
284 }
285 }
286 return -ENODEV;
287}
288
289/* update dapm codec register bits */
290static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
291{
292 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300293 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200294 struct snd_soc_codec *codec = widget->codec;
295
296 /* check for valid widgets */
297 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
298 widget->id == snd_soc_dapm_output ||
299 widget->id == snd_soc_dapm_hp ||
300 widget->id == snd_soc_dapm_mic ||
301 widget->id == snd_soc_dapm_line ||
302 widget->id == snd_soc_dapm_spk)
303 return 0;
304
305 power = widget->power;
306 if (widget->invert)
307 power = (power ? 0:1);
308
309 old = snd_soc_read(codec, widget->reg);
310 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
311
312 change = old != new;
313 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700314 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
315 widget->name, widget->power ? "on" : "off",
316 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700318 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 }
Mark Brownc1286b82008-07-07 19:26:03 +0100320 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
321 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200322 return change;
323}
324
325/* ramps the volume up or down to minimise pops before or after a
326 * DAPM power event */
327static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
328{
329 const struct snd_kcontrol_new *k = widget->kcontrols;
330
331 if (widget->muted && !power)
332 return 0;
333 if (!widget->muted && power)
334 return 0;
335
336 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100337 struct soc_mixer_control *mc =
338 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400339 unsigned int reg = mc->reg;
340 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100341 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400342 unsigned int mask = (1 << fls(max)) - 1;
343 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200344
345 if (power) {
346 int i;
347 /* power up has happended, increase volume to last level */
348 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100349 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200350 snd_soc_update_bits(widget->codec, reg, mask, i);
351 } else {
352 for (i = 0; i < widget->saved_value; i++)
353 snd_soc_update_bits(widget->codec, reg, mask, i);
354 }
355 widget->muted = 0;
356 } else {
357 /* power down is about to occur, decrease volume to mute */
358 int val = snd_soc_read(widget->codec, reg);
359 int i = widget->saved_value = (val >> shift) & mask;
360 if (invert) {
361 for (; i < mask; i++)
362 snd_soc_update_bits(widget->codec, reg, mask, i);
363 } else {
364 for (; i > 0; i--)
365 snd_soc_update_bits(widget->codec, reg, mask, i);
366 }
367 widget->muted = 1;
368 }
369 }
370 return 0;
371}
372
373/* create new dapm mixer control */
374static int dapm_new_mixer(struct snd_soc_codec *codec,
375 struct snd_soc_dapm_widget *w)
376{
377 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000378 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200379 struct snd_soc_dapm_path *path;
380
381 /* add kcontrol */
382 for (i = 0; i < w->num_kcontrols; i++) {
383
384 /* match name */
385 list_for_each_entry(path, &w->sources, list_sink) {
386
387 /* mixer/mux paths name must match control name */
388 if (path->name != (char*)w->kcontrols[i].name)
389 continue;
390
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000391 /* add dapm control with long name.
392 * for dapm_mixer this is the concatenation of the
393 * mixer and kcontrol name.
394 * for dapm_mixer_named_ctl this is simply the
395 * kcontrol name.
396 */
397 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000398 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000399 name_len += 1 + strlen(w->name);
400
Mark Brown219b93f2008-10-28 13:02:31 +0000401 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000402
Richard Purdie2b97eab2006-10-06 18:32:18 +0200403 if (path->long_name == NULL)
404 return -ENOMEM;
405
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000406 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000407 default:
408 snprintf(path->long_name, name_len, "%s %s",
409 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000410 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000411 case snd_soc_dapm_mixer_named_ctl:
412 snprintf(path->long_name, name_len, "%s",
413 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000414 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000415 }
416
Mark Brown219b93f2008-10-28 13:02:31 +0000417 path->long_name[name_len - 1] = '\0';
418
Richard Purdie2b97eab2006-10-06 18:32:18 +0200419 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
420 path->long_name);
421 ret = snd_ctl_add(codec->card, path->kcontrol);
422 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100423 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
424 path->long_name,
425 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 kfree(path->long_name);
427 path->long_name = NULL;
428 return ret;
429 }
430 }
431 }
432 return ret;
433}
434
435/* create new dapm mux control */
436static int dapm_new_mux(struct snd_soc_codec *codec,
437 struct snd_soc_dapm_widget *w)
438{
439 struct snd_soc_dapm_path *path = NULL;
440 struct snd_kcontrol *kcontrol;
441 int ret = 0;
442
443 if (!w->num_kcontrols) {
444 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
445 return -EINVAL;
446 }
447
448 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
449 ret = snd_ctl_add(codec->card, kcontrol);
450 if (ret < 0)
451 goto err;
452
453 list_for_each_entry(path, &w->sources, list_sink)
454 path->kcontrol = kcontrol;
455
456 return ret;
457
458err:
459 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
460 return ret;
461}
462
463/* create new dapm volume control */
464static int dapm_new_pga(struct snd_soc_codec *codec,
465 struct snd_soc_dapm_widget *w)
466{
467 struct snd_kcontrol *kcontrol;
468 int ret = 0;
469
470 if (!w->num_kcontrols)
471 return -EINVAL;
472
473 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
474 ret = snd_ctl_add(codec->card, kcontrol);
475 if (ret < 0) {
476 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
477 return ret;
478 }
479
480 return ret;
481}
482
483/* reset 'walked' bit for each dapm path */
484static inline void dapm_clear_walk(struct snd_soc_codec *codec)
485{
486 struct snd_soc_dapm_path *p;
487
488 list_for_each_entry(p, &codec->dapm_paths, list)
489 p->walked = 0;
490}
491
492/*
493 * Recursively check for a completed path to an active or physically connected
494 * output widget. Returns number of complete paths.
495 */
496static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
497{
498 struct snd_soc_dapm_path *path;
499 int con = 0;
500
Mark Brown246d0a12009-04-22 18:24:55 +0100501 if (widget->id == snd_soc_dapm_supply)
502 return 0;
503
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 if (widget->id == snd_soc_dapm_adc && widget->active)
505 return 1;
506
507 if (widget->connected) {
508 /* connected pin ? */
509 if (widget->id == snd_soc_dapm_output && !widget->ext)
510 return 1;
511
512 /* connected jack or spk ? */
513 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
514 widget->id == snd_soc_dapm_line)
515 return 1;
516 }
517
518 list_for_each_entry(path, &widget->sinks, list_source) {
519 if (path->walked)
520 continue;
521
522 if (path->sink && path->connect) {
523 path->walked = 1;
524 con += is_connected_output_ep(path->sink);
525 }
526 }
527
528 return con;
529}
530
531/*
532 * Recursively check for a completed path to an active or physically connected
533 * input widget. Returns number of complete paths.
534 */
535static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
536{
537 struct snd_soc_dapm_path *path;
538 int con = 0;
539
Mark Brown246d0a12009-04-22 18:24:55 +0100540 if (widget->id == snd_soc_dapm_supply)
541 return 0;
542
Richard Purdie2b97eab2006-10-06 18:32:18 +0200543 /* active stream ? */
544 if (widget->id == snd_soc_dapm_dac && widget->active)
545 return 1;
546
547 if (widget->connected) {
548 /* connected pin ? */
549 if (widget->id == snd_soc_dapm_input && !widget->ext)
550 return 1;
551
552 /* connected VMID/Bias for lower pops */
553 if (widget->id == snd_soc_dapm_vmid)
554 return 1;
555
556 /* connected jack ? */
557 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
558 return 1;
559 }
560
561 list_for_each_entry(path, &widget->sources, list_sink) {
562 if (path->walked)
563 continue;
564
565 if (path->source && path->connect) {
566 path->walked = 1;
567 con += is_connected_input_ep(path->source);
568 }
569 }
570
571 return con;
572}
573
574/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300575 * Handler for generic register modifier widget.
576 */
577int dapm_reg_event(struct snd_soc_dapm_widget *w,
578 struct snd_kcontrol *kcontrol, int event)
579{
580 unsigned int val;
581
582 if (SND_SOC_DAPM_EVENT_ON(event))
583 val = w->on_val;
584 else
585 val = w->off_val;
586
587 snd_soc_update_bits(w->codec, -(w->reg + 1),
588 w->mask << w->shift, val << w->shift);
589
590 return 0;
591}
Mark Brown11589412008-07-29 11:42:23 +0100592EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300593
Mark Brown025756e2009-04-13 11:09:18 +0100594/* Standard power change method, used to apply power changes to most
595 * widgets.
596 */
597static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
598{
599 int ret;
600
601 /* call any power change event handlers */
602 if (w->event)
603 pr_debug("power %s event for %s flags %x\n",
604 w->power ? "on" : "off",
605 w->name, w->event_flags);
606
607 /* power up pre event */
608 if (w->power && w->event &&
609 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
610 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
611 if (ret < 0)
612 return ret;
613 }
614
615 /* power down pre event */
616 if (!w->power && w->event &&
617 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
618 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
619 if (ret < 0)
620 return ret;
621 }
622
623 /* Lower PGA volume to reduce pops */
624 if (w->id == snd_soc_dapm_pga && !w->power)
625 dapm_set_pga(w, w->power);
626
627 dapm_update_bits(w);
628
629 /* Raise PGA volume to reduce pops */
630 if (w->id == snd_soc_dapm_pga && w->power)
631 dapm_set_pga(w, w->power);
632
633 /* power up post event */
634 if (w->power && w->event &&
635 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
636 ret = w->event(w,
637 NULL, SND_SOC_DAPM_POST_PMU);
638 if (ret < 0)
639 return ret;
640 }
641
642 /* power down post event */
643 if (!w->power && w->event &&
644 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
645 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
646 if (ret < 0)
647 return ret;
648 }
649
650 return 0;
651}
652
Mark Browncd0f2d42009-04-20 16:56:59 +0100653/* Generic check to see if a widget should be powered.
654 */
655static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
656{
657 int in, out;
658
659 in = is_connected_input_ep(w);
660 dapm_clear_walk(w->codec);
661 out = is_connected_output_ep(w);
662 dapm_clear_walk(w->codec);
663 return out != 0 && in != 0;
664}
665
Mark Brown6ea31b92009-04-20 17:15:41 +0100666/* Check to see if an ADC has power */
667static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
668{
669 int in;
670
671 if (w->active) {
672 in = is_connected_input_ep(w);
673 dapm_clear_walk(w->codec);
674 return in != 0;
675 } else {
676 return dapm_generic_check_power(w);
677 }
678}
679
680/* Check to see if a DAC has power */
681static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
682{
683 int out;
684
685 if (w->active) {
686 out = is_connected_output_ep(w);
687 dapm_clear_walk(w->codec);
688 return out != 0;
689 } else {
690 return dapm_generic_check_power(w);
691 }
692}
693
Mark Brown246d0a12009-04-22 18:24:55 +0100694/* Check to see if a power supply is needed */
695static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
696{
697 struct snd_soc_dapm_path *path;
698 int power = 0;
699
700 /* Check if one of our outputs is connected */
701 list_for_each_entry(path, &w->sinks, list_source) {
702 if (path->sink && path->sink->power_check &&
703 path->sink->power_check(path->sink)) {
704 power = 1;
705 break;
706 }
707 }
708
709 dapm_clear_walk(w->codec);
710
711 return power;
712}
713
Mark Brown38357ab2009-06-06 19:03:23 +0100714static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
715 struct snd_soc_dapm_widget *b,
716 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000717{
Mark Brown38357ab2009-06-06 19:03:23 +0100718 if (sort[a->id] != sort[b->id])
719 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100720 if (a->reg != b->reg)
721 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000722
Mark Brown38357ab2009-06-06 19:03:23 +0100723 return 0;
724}
Mark Brown42aa3412009-03-01 19:21:10 +0000725
Mark Brown38357ab2009-06-06 19:03:23 +0100726/* Insert a widget in order into a DAPM power sequence. */
727static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
728 struct list_head *list,
729 int sort[])
730{
731 struct snd_soc_dapm_widget *w;
732
733 list_for_each_entry(w, list, power_list)
734 if (dapm_seq_compare(new_widget, w, sort) < 0) {
735 list_add_tail(&new_widget->power_list, &w->power_list);
736 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000737 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100738
Mark Brown38357ab2009-06-06 19:03:23 +0100739 list_add_tail(&new_widget->power_list, list);
740}
Mark Brown42aa3412009-03-01 19:21:10 +0000741
Mark Brownb22ead22009-06-07 12:51:26 +0100742/* Apply the coalesced changes from a DAPM sequence */
743static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
744 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100745{
746 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100747 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100748 unsigned int value = 0;
749 unsigned int mask = 0;
750 unsigned int cur_mask;
751
752 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
753 power_list)->reg;
754
755 list_for_each_entry(w, pending, power_list) {
756 cur_mask = 1 << w->shift;
757 BUG_ON(reg != w->reg);
758
759 if (w->invert)
760 power = !w->power;
761 else
762 power = w->power;
763
764 mask |= cur_mask;
765 if (power)
766 value |= cur_mask;
767
768 pop_dbg(codec->pop_time,
769 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
770 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100771
772 /* power up pre event */
773 if (w->power && w->event &&
774 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
775 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
776 w->name);
777 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
778 if (ret < 0)
779 pr_err("%s: pre event failed: %d\n",
780 w->name, ret);
781 }
782
783 /* power down pre event */
784 if (!w->power && w->event &&
785 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
786 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
787 w->name);
788 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
789 if (ret < 0)
790 pr_err("%s: pre event failed: %d\n",
791 w->name, ret);
792 }
Mark Brown4f1c1922009-06-07 13:37:44 +0100793
794 /* Lower PGA volume to reduce pops */
795 if (w->id == snd_soc_dapm_pga && !w->power)
796 dapm_set_pga(w, w->power);
Mark Brownb22ead22009-06-07 12:51:26 +0100797 }
798
Mark Brown81628102009-06-07 13:21:24 +0100799 if (reg >= 0) {
800 pop_dbg(codec->pop_time,
801 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
802 value, mask, reg, codec->pop_time);
803 pop_wait(codec->pop_time);
804 snd_soc_update_bits(codec, reg, mask, value);
805 }
806
807 list_for_each_entry(w, pending, power_list) {
Mark Brown4f1c1922009-06-07 13:37:44 +0100808 /* Raise PGA volume to reduce pops */
809 if (w->id == snd_soc_dapm_pga && w->power)
810 dapm_set_pga(w, w->power);
811
Mark Brown81628102009-06-07 13:21:24 +0100812 /* power up post event */
813 if (w->power && w->event &&
814 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
815 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
816 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000817 ret = w->event(w,
818 NULL, SND_SOC_DAPM_POST_PMU);
819 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100820 pr_err("%s: post event failed: %d\n",
821 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000822 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100823
Mark Brown81628102009-06-07 13:21:24 +0100824 /* power down post event */
825 if (!w->power && w->event &&
826 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
827 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
828 w->name);
829 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
830 if (ret < 0)
831 pr_err("%s: post event failed: %d\n",
832 w->name, ret);
833 }
Mark Brown42aa3412009-03-01 19:21:10 +0000834 }
Mark Brown42aa3412009-03-01 19:21:10 +0000835}
836
Mark Brownb22ead22009-06-07 12:51:26 +0100837/* Apply a DAPM power sequence.
838 *
839 * We walk over a pre-sorted list of widgets to apply power to. In
840 * order to minimise the number of writes to the device required
841 * multiple widgets will be updated in a single write where possible.
842 * Currently anything that requires more than a single write is not
843 * handled.
844 */
845static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
846 int event, int sort[])
847{
848 struct snd_soc_dapm_widget *w, *n;
849 LIST_HEAD(pending);
850 int cur_sort = -1;
851 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100852 int ret;
853
Mark Brownb22ead22009-06-07 12:51:26 +0100854 list_for_each_entry_safe(w, n, list, power_list) {
855 ret = 0;
856
857 /* Do we need to apply any queued changes? */
858 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
859 if (!list_empty(&pending))
860 dapm_seq_run_coalesced(codec, &pending);
861
862 INIT_LIST_HEAD(&pending);
863 cur_sort = -1;
864 cur_reg = SND_SOC_NOPM;
865 }
866
Mark Brown163cac02009-06-07 10:12:52 +0100867 switch (w->id) {
868 case snd_soc_dapm_pre:
869 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100870 list_for_each_entry_safe_continue(w, n, list,
871 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100872
Mark Brownb22ead22009-06-07 12:51:26 +0100873 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100874 ret = w->event(w,
875 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100876 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100877 ret = w->event(w,
878 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100879 break;
880
881 case snd_soc_dapm_post:
882 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100883 list_for_each_entry_safe_continue(w, n, list,
884 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100885
Mark Brownb22ead22009-06-07 12:51:26 +0100886 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100887 ret = w->event(w,
888 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100889 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100890 ret = w->event(w,
891 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100892 break;
893
894 case snd_soc_dapm_input:
895 case snd_soc_dapm_output:
896 case snd_soc_dapm_hp:
897 case snd_soc_dapm_mic:
898 case snd_soc_dapm_line:
899 case snd_soc_dapm_spk:
900 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100901 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100902 break;
903
904 default:
Mark Brown81628102009-06-07 13:21:24 +0100905 /* Queue it up for application */
906 cur_sort = sort[w->id];
907 cur_reg = w->reg;
908 list_move(&w->power_list, &pending);
909 break;
Mark Brown163cac02009-06-07 10:12:52 +0100910 }
Mark Brownb22ead22009-06-07 12:51:26 +0100911
912 if (ret < 0)
913 pr_err("Failed to apply widget power: %d\n",
914 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100915 }
Mark Brownb22ead22009-06-07 12:51:26 +0100916
917 if (!list_empty(&pending))
918 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100919}
920
Mark Brown42aa3412009-03-01 19:21:10 +0000921/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922 * Scan each dapm widget for complete audio path.
923 * A complete path is a route that has valid endpoints i.e.:-
924 *
925 * o DAC to output pin.
926 * o Input Pin to ADC.
927 * o Input pin to Output pin (bypass, sidetone)
928 * o DAC to ADC (loopback).
929 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100930static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931{
Mark Brown452c5ea2009-05-17 21:41:23 +0100932 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100934 LIST_HEAD(up_list);
935 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100936 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100937 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100938 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200939
Mark Brown6d3ddc82009-05-16 17:47:29 +0100940 /* Check which widgets we need to power and store them in
941 * lists indicating if they should be powered up or down.
942 */
943 list_for_each_entry(w, &codec->dapm_widgets, list) {
944 switch (w->id) {
945 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100946 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100947 break;
948 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100949 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100950 break;
951
952 default:
953 if (!w->power_check)
954 continue;
955
956 power = w->power_check(w);
Mark Brown452c5ea2009-05-17 21:41:23 +0100957 if (power)
958 sys_power = 1;
959
Mark Brown6d3ddc82009-05-16 17:47:29 +0100960 if (w->power == power)
961 continue;
962
963 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100964 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100965 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100966 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100967
968 w->power = power;
969 break;
970 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971 }
972
Mark Brownb14b76a2009-08-17 11:55:38 +0100973 /* If there are no DAPM widgets then try to figure out power from the
974 * event type.
975 */
976 if (list_empty(&codec->dapm_widgets)) {
977 switch (event) {
978 case SND_SOC_DAPM_STREAM_START:
979 case SND_SOC_DAPM_STREAM_RESUME:
980 sys_power = 1;
981 break;
982 case SND_SOC_DAPM_STREAM_NOP:
983 sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
984 default:
985 break;
986 }
987 }
988
Mark Brown452c5ea2009-05-17 21:41:23 +0100989 /* If we're changing to all on or all off then prepare */
990 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
991 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
992 ret = snd_soc_dapm_set_bias_level(socdev,
993 SND_SOC_BIAS_PREPARE);
994 if (ret != 0)
995 pr_err("Failed to prepare bias: %d\n", ret);
996 }
997
Mark Brown6d3ddc82009-05-16 17:47:29 +0100998 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100999 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001000
1001 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001002 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003
Mark Brown452c5ea2009-05-17 21:41:23 +01001004 /* If we just powered the last thing off drop to standby bias */
1005 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1006 ret = snd_soc_dapm_set_bias_level(socdev,
1007 SND_SOC_BIAS_STANDBY);
1008 if (ret != 0)
1009 pr_err("Failed to apply standby bias: %d\n", ret);
1010 }
1011
1012 /* If we just powered up then move to active bias */
1013 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1014 ret = snd_soc_dapm_set_bias_level(socdev,
1015 SND_SOC_BIAS_ON);
1016 if (ret != 0)
1017 pr_err("Failed to apply active bias: %d\n", ret);
1018 }
1019
Mark Browncb507e72009-07-08 18:54:57 +01001020 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1021 codec->pop_time);
1022
Mark Brown42aa3412009-03-01 19:21:10 +00001023 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001024}
1025
Mark Brownc1286b82008-07-07 19:26:03 +01001026#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +02001027static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
1028{
1029 struct snd_soc_dapm_widget *w;
1030 struct snd_soc_dapm_path *p = NULL;
1031 int in, out;
1032
1033 printk("DAPM %s %s\n", codec->name, action);
1034
1035 list_for_each_entry(w, &codec->dapm_widgets, list) {
1036
1037 /* only display widgets that effect routing */
1038 switch (w->id) {
1039 case snd_soc_dapm_pre:
1040 case snd_soc_dapm_post:
1041 case snd_soc_dapm_vmid:
1042 continue;
1043 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001044 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001045 case snd_soc_dapm_output:
1046 case snd_soc_dapm_input:
1047 case snd_soc_dapm_switch:
1048 case snd_soc_dapm_hp:
1049 case snd_soc_dapm_mic:
1050 case snd_soc_dapm_spk:
1051 case snd_soc_dapm_line:
1052 case snd_soc_dapm_micbias:
1053 case snd_soc_dapm_dac:
1054 case snd_soc_dapm_adc:
1055 case snd_soc_dapm_pga:
1056 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001057 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001058 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001059 if (w->name) {
1060 in = is_connected_input_ep(w);
1061 dapm_clear_walk(w->codec);
1062 out = is_connected_output_ep(w);
1063 dapm_clear_walk(w->codec);
1064 printk("%s: %s in %d out %d\n", w->name,
1065 w->power ? "On":"Off",in, out);
1066
1067 list_for_each_entry(p, &w->sources, list_sink) {
1068 if (p->connect)
1069 printk(" in %s %s\n", p->name ? p->name : "static",
1070 p->source->name);
1071 }
1072 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001073 if (p->connect)
1074 printk(" out %s %s\n", p->name ? p->name : "static",
1075 p->sink->name);
1076 }
1077 }
1078 break;
1079 }
1080 }
1081}
1082#endif
1083
Mark Brown79fb9382009-08-21 16:38:13 +01001084#ifdef CONFIG_DEBUG_FS
1085static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1086{
1087 file->private_data = inode->i_private;
1088 return 0;
1089}
1090
1091static ssize_t dapm_widget_power_read_file(struct file *file,
1092 char __user *user_buf,
1093 size_t count, loff_t *ppos)
1094{
1095 struct snd_soc_dapm_widget *w = file->private_data;
1096 char *buf;
1097 int in, out;
1098 ssize_t ret;
1099 struct snd_soc_dapm_path *p = NULL;
1100
1101 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1102 if (!buf)
1103 return -ENOMEM;
1104
1105 in = is_connected_input_ep(w);
1106 dapm_clear_walk(w->codec);
1107 out = is_connected_output_ep(w);
1108 dapm_clear_walk(w->codec);
1109
1110 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d\n",
1111 w->name, w->power ? "On" : "Off", in, out);
1112
1113 if (w->active && w->sname)
1114 ret += snprintf(buf, PAGE_SIZE - ret, " stream %s active\n",
1115 w->sname);
1116
1117 list_for_each_entry(p, &w->sources, list_sink) {
1118 if (p->connect)
1119 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1120 " in %s %s\n",
1121 p->name ? p->name : "static",
1122 p->source->name);
1123 }
1124 list_for_each_entry(p, &w->sinks, list_source) {
1125 if (p->connect)
1126 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1127 " out %s %s\n",
1128 p->name ? p->name : "static",
1129 p->sink->name);
1130 }
1131
1132 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1133
1134 kfree(buf);
1135 return ret;
1136}
1137
1138static const struct file_operations dapm_widget_power_fops = {
1139 .open = dapm_widget_power_open_file,
1140 .read = dapm_widget_power_read_file,
1141};
1142
1143void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1144{
1145 struct snd_soc_dapm_widget *w;
1146 struct dentry *d;
1147
1148 if (!codec->debugfs_dapm)
1149 return;
1150
1151 list_for_each_entry(w, &codec->dapm_widgets, list) {
1152 if (!w->name)
1153 continue;
1154
1155 d = debugfs_create_file(w->name, 0444,
1156 codec->debugfs_dapm, w,
1157 &dapm_widget_power_fops);
1158 if (!d)
1159 printk(KERN_WARNING
1160 "ASoC: Failed to create %s debugfs file\n",
1161 w->name);
1162 }
1163}
1164#else
1165void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1166{
1167}
1168#endif
1169
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001171static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1172 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001173 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001174{
1175 struct snd_soc_dapm_path *path;
1176 int found = 0;
1177
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001178 if (widget->id != snd_soc_dapm_mux &&
1179 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001180 return -ENODEV;
1181
1182 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
1183 return 0;
1184
1185 /* find dapm widget path assoc with kcontrol */
1186 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1187 if (path->kcontrol != kcontrol)
1188 continue;
1189
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001190 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001191 continue;
1192
1193 found = 1;
1194 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001195 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001196 path->connect = 1; /* new connection */
1197 else
1198 path->connect = 0; /* old connection must be powered down */
1199 }
1200
Mark Brown3fccd8b2008-07-07 19:26:04 +01001201 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001202 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001203 dump_dapm(widget->codec, "mux power update");
1204 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001205
1206 return 0;
1207}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001208
Milan plzik1b075e32008-01-10 14:39:46 +01001209/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001210static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1211 struct snd_kcontrol *kcontrol, int reg,
1212 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001213{
1214 struct snd_soc_dapm_path *path;
1215 int found = 0;
1216
Milan plzik1b075e32008-01-10 14:39:46 +01001217 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001218 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001219 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001220 return -ENODEV;
1221
1222 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
1223 return 0;
1224
1225 /* find dapm widget path assoc with kcontrol */
1226 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1227 if (path->kcontrol != kcontrol)
1228 continue;
1229
1230 /* found, now check type */
1231 found = 1;
1232 if (val)
1233 /* new connection */
1234 path->connect = invert ? 0:1;
1235 else
1236 /* old connection must be powered down */
1237 path->connect = invert ? 1:0;
1238 break;
1239 }
1240
Mark Brown3fccd8b2008-07-07 19:26:04 +01001241 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001242 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001243 dump_dapm(widget->codec, "mixer power update");
1244 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001245
1246 return 0;
1247}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001248
1249/* show dapm widget status in sys fs */
1250static ssize_t dapm_widget_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1252{
1253 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001254 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001255 struct snd_soc_dapm_widget *w;
1256 int count = 0;
1257 char *state = "not set";
1258
1259 list_for_each_entry(w, &codec->dapm_widgets, list) {
1260
1261 /* only display widgets that burnm power */
1262 switch (w->id) {
1263 case snd_soc_dapm_hp:
1264 case snd_soc_dapm_mic:
1265 case snd_soc_dapm_spk:
1266 case snd_soc_dapm_line:
1267 case snd_soc_dapm_micbias:
1268 case snd_soc_dapm_dac:
1269 case snd_soc_dapm_adc:
1270 case snd_soc_dapm_pga:
1271 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001272 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001273 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001274 if (w->name)
1275 count += sprintf(buf + count, "%s: %s\n",
1276 w->name, w->power ? "On":"Off");
1277 break;
1278 default:
1279 break;
1280 }
1281 }
1282
Mark Brown0be98982008-05-19 12:31:28 +02001283 switch (codec->bias_level) {
1284 case SND_SOC_BIAS_ON:
1285 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001286 break;
Mark Brown0be98982008-05-19 12:31:28 +02001287 case SND_SOC_BIAS_PREPARE:
1288 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289 break;
Mark Brown0be98982008-05-19 12:31:28 +02001290 case SND_SOC_BIAS_STANDBY:
1291 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001292 break;
Mark Brown0be98982008-05-19 12:31:28 +02001293 case SND_SOC_BIAS_OFF:
1294 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001295 break;
1296 }
1297 count += sprintf(buf + count, "PM State: %s\n", state);
1298
1299 return count;
1300}
1301
1302static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1303
1304int snd_soc_dapm_sys_add(struct device *dev)
1305{
Troy Kisky12ef1932008-10-13 17:42:14 -07001306 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001307}
1308
1309static void snd_soc_dapm_sys_remove(struct device *dev)
1310{
Mark Brownaef90842009-05-16 17:53:16 +01001311 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312}
1313
1314/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001315static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316{
1317 struct snd_soc_dapm_widget *w, *next_w;
1318 struct snd_soc_dapm_path *p, *next_p;
1319
1320 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1321 list_del(&w->list);
1322 kfree(w);
1323 }
1324
1325 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1326 list_del(&p->list);
1327 kfree(p->long_name);
1328 kfree(p);
1329 }
1330}
1331
Liam Girdwooda5302182008-07-07 13:35:17 +01001332static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001333 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001334{
1335 struct snd_soc_dapm_widget *w;
1336
1337 list_for_each_entry(w, &codec->dapm_widgets, list) {
1338 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001339 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001340 w->connected = status;
1341 return 0;
1342 }
1343 }
1344
Mark Brownc1286b82008-07-07 19:26:03 +01001345 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001346 return -EINVAL;
1347}
1348
Richard Purdie2b97eab2006-10-06 18:32:18 +02001349/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001350 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001351 * @codec: audio codec
1352 *
1353 * Walks all dapm audio paths and powers widgets according to their
1354 * stream or path usage.
1355 *
1356 * Returns 0 for success.
1357 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001358int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001359{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001360 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1361 dump_dapm(codec, "sync");
1362 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001363}
Liam Girdwooda5302182008-07-07 13:35:17 +01001364EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365
Mark Brown105f1c22008-05-13 14:52:19 +02001366static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1367 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001368{
1369 struct snd_soc_dapm_path *path;
1370 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1371 int ret = 0;
1372
1373 /* find src and dest widgets */
1374 list_for_each_entry(w, &codec->dapm_widgets, list) {
1375
1376 if (!wsink && !(strcmp(w->name, sink))) {
1377 wsink = w;
1378 continue;
1379 }
1380 if (!wsource && !(strcmp(w->name, source))) {
1381 wsource = w;
1382 }
1383 }
1384
1385 if (wsource == NULL || wsink == NULL)
1386 return -ENODEV;
1387
1388 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1389 if (!path)
1390 return -ENOMEM;
1391
1392 path->source = wsource;
1393 path->sink = wsink;
1394 INIT_LIST_HEAD(&path->list);
1395 INIT_LIST_HEAD(&path->list_source);
1396 INIT_LIST_HEAD(&path->list_sink);
1397
1398 /* check for external widgets */
1399 if (wsink->id == snd_soc_dapm_input) {
1400 if (wsource->id == snd_soc_dapm_micbias ||
1401 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001402 wsource->id == snd_soc_dapm_line ||
1403 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404 wsink->ext = 1;
1405 }
1406 if (wsource->id == snd_soc_dapm_output) {
1407 if (wsink->id == snd_soc_dapm_spk ||
1408 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001409 wsink->id == snd_soc_dapm_line ||
1410 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001411 wsource->ext = 1;
1412 }
1413
1414 /* connect static paths */
1415 if (control == NULL) {
1416 list_add(&path->list, &codec->dapm_paths);
1417 list_add(&path->list_sink, &wsink->sources);
1418 list_add(&path->list_source, &wsource->sinks);
1419 path->connect = 1;
1420 return 0;
1421 }
1422
1423 /* connect dynamic paths */
1424 switch(wsink->id) {
1425 case snd_soc_dapm_adc:
1426 case snd_soc_dapm_dac:
1427 case snd_soc_dapm_pga:
1428 case snd_soc_dapm_input:
1429 case snd_soc_dapm_output:
1430 case snd_soc_dapm_micbias:
1431 case snd_soc_dapm_vmid:
1432 case snd_soc_dapm_pre:
1433 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001434 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435 list_add(&path->list, &codec->dapm_paths);
1436 list_add(&path->list_sink, &wsink->sources);
1437 list_add(&path->list_source, &wsource->sinks);
1438 path->connect = 1;
1439 return 0;
1440 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001441 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001442 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1443 &wsink->kcontrols[0]);
1444 if (ret != 0)
1445 goto err;
1446 break;
1447 case snd_soc_dapm_switch:
1448 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001449 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1451 if (ret != 0)
1452 goto err;
1453 break;
1454 case snd_soc_dapm_hp:
1455 case snd_soc_dapm_mic:
1456 case snd_soc_dapm_line:
1457 case snd_soc_dapm_spk:
1458 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 = 0;
1462 return 0;
1463 }
1464 return 0;
1465
1466err:
1467 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1468 control, sink);
1469 kfree(path);
1470 return ret;
1471}
Mark Brown105f1c22008-05-13 14:52:19 +02001472
1473/**
Mark Brown105f1c22008-05-13 14:52:19 +02001474 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1475 * @codec: codec
1476 * @route: audio routes
1477 * @num: number of routes
1478 *
1479 * Connects 2 dapm widgets together via a named audio path. The sink is
1480 * the widget receiving the audio signal, whilst the source is the sender
1481 * of the audio signal.
1482 *
1483 * Returns 0 for success else error. On error all resources can be freed
1484 * with a call to snd_soc_card_free().
1485 */
1486int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1487 const struct snd_soc_dapm_route *route, int num)
1488{
1489 int i, ret;
1490
1491 for (i = 0; i < num; i++) {
1492 ret = snd_soc_dapm_add_route(codec, route->sink,
1493 route->control, route->source);
1494 if (ret < 0) {
1495 printk(KERN_ERR "Failed to add route %s->%s\n",
1496 route->source,
1497 route->sink);
1498 return ret;
1499 }
1500 route++;
1501 }
1502
1503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1506
1507/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001508 * snd_soc_dapm_new_widgets - add new dapm widgets
1509 * @codec: audio codec
1510 *
1511 * Checks the codec for any new dapm widgets and creates them if found.
1512 *
1513 * Returns 0 for success.
1514 */
1515int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1516{
1517 struct snd_soc_dapm_widget *w;
1518
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 list_for_each_entry(w, &codec->dapm_widgets, list)
1520 {
1521 if (w->new)
1522 continue;
1523
1524 switch(w->id) {
1525 case snd_soc_dapm_switch:
1526 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001527 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001528 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529 dapm_new_mixer(codec, w);
1530 break;
1531 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001532 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001533 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534 dapm_new_mux(codec, w);
1535 break;
1536 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001537 w->power_check = dapm_adc_check_power;
1538 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001539 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001540 w->power_check = dapm_dac_check_power;
1541 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001543 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544 dapm_new_pga(codec, w);
1545 break;
1546 case snd_soc_dapm_input:
1547 case snd_soc_dapm_output:
1548 case snd_soc_dapm_micbias:
1549 case snd_soc_dapm_spk:
1550 case snd_soc_dapm_hp:
1551 case snd_soc_dapm_mic:
1552 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001553 w->power_check = dapm_generic_check_power;
1554 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001555 case snd_soc_dapm_supply:
1556 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557 case snd_soc_dapm_vmid:
1558 case snd_soc_dapm_pre:
1559 case snd_soc_dapm_post:
1560 break;
1561 }
1562 w->new = 1;
1563 }
1564
1565 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001566 return 0;
1567}
1568EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1569
1570/**
1571 * snd_soc_dapm_get_volsw - dapm mixer get callback
1572 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001573 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001574 *
1575 * Callback to get the value of a dapm mixer control.
1576 *
1577 * Returns 0 for success.
1578 */
1579int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
1581{
1582 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001583 struct soc_mixer_control *mc =
1584 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001585 unsigned int reg = mc->reg;
1586 unsigned int shift = mc->shift;
1587 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001588 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001589 unsigned int invert = mc->invert;
1590 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001591
1592 /* return the saved value if we are powered down */
1593 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1594 ucontrol->value.integer.value[0] = widget->saved_value;
1595 return 0;
1596 }
1597
1598 ucontrol->value.integer.value[0] =
1599 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1600 if (shift != rshift)
1601 ucontrol->value.integer.value[1] =
1602 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1603 if (invert) {
1604 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001605 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001606 if (shift != rshift)
1607 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001608 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001609 }
1610
1611 return 0;
1612}
1613EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1614
1615/**
1616 * snd_soc_dapm_put_volsw - dapm mixer set callback
1617 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001618 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001619 *
1620 * Callback to set the value of a dapm mixer control.
1621 *
1622 * Returns 0 for success.
1623 */
1624int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1626{
1627 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001628 struct soc_mixer_control *mc =
1629 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001630 unsigned int reg = mc->reg;
1631 unsigned int shift = mc->shift;
1632 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001633 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001634 unsigned int mask = (1 << fls(max)) - 1;
1635 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001636 unsigned int val, val2, val_mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001637 int ret;
1638
1639 val = (ucontrol->value.integer.value[0] & mask);
1640
1641 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001642 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643 val_mask = mask << shift;
1644 val = val << shift;
1645 if (shift != rshift) {
1646 val2 = (ucontrol->value.integer.value[1] & mask);
1647 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001648 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649 val_mask |= mask << rshift;
1650 val |= val2 << rshift;
1651 }
1652
1653 mutex_lock(&widget->codec->mutex);
1654 widget->value = val;
1655
1656 /* save volume value if the widget is powered down */
1657 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1658 widget->saved_value = val;
1659 mutex_unlock(&widget->codec->mutex);
1660 return 1;
1661 }
1662
1663 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1664 if (widget->event) {
1665 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001666 ret = widget->event(widget, kcontrol,
1667 SND_SOC_DAPM_PRE_REG);
1668 if (ret < 0) {
1669 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001671 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672 }
1673 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1674 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001675 ret = widget->event(widget, kcontrol,
1676 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677 } else
1678 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1679
1680out:
1681 mutex_unlock(&widget->codec->mutex);
1682 return ret;
1683}
1684EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1685
1686/**
1687 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1688 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001689 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 *
1691 * Callback to get the value of a dapm enumerated double mixer control.
1692 *
1693 * Returns 0 for success.
1694 */
1695int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol)
1697{
1698 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1699 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001700 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001701
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001702 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 ;
1704 val = snd_soc_read(widget->codec, e->reg);
1705 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1706 if (e->shift_l != e->shift_r)
1707 ucontrol->value.enumerated.item[1] =
1708 (val >> e->shift_r) & (bitmask - 1);
1709
1710 return 0;
1711}
1712EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1713
1714/**
1715 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1716 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001717 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 *
1719 * Callback to set the value of a dapm enumerated double mixer control.
1720 *
1721 * Returns 0 for success.
1722 */
1723int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_value *ucontrol)
1725{
1726 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1727 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001728 unsigned int val, mux;
1729 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 int ret = 0;
1731
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001732 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001733 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001734 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 return -EINVAL;
1736 mux = ucontrol->value.enumerated.item[0];
1737 val = mux << e->shift_l;
1738 mask = (bitmask - 1) << e->shift_l;
1739 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001740 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001741 return -EINVAL;
1742 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1743 mask |= (bitmask - 1) << e->shift_r;
1744 }
1745
1746 mutex_lock(&widget->codec->mutex);
1747 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001748 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749 if (widget->event) {
1750 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001751 ret = widget->event(widget,
1752 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 if (ret < 0)
1754 goto out;
1755 }
1756 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1757 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001758 ret = widget->event(widget,
1759 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760 } else
1761 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1762
1763out:
1764 mutex_unlock(&widget->codec->mutex);
1765 return ret;
1766}
1767EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1768
1769/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001770 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1771 * callback
1772 * @kcontrol: mixer control
1773 * @ucontrol: control element information
1774 *
1775 * Callback to get the value of a dapm semi enumerated double mixer control.
1776 *
1777 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1778 * used for handling bitfield coded enumeration for example.
1779 *
1780 * Returns 0 for success.
1781 */
1782int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_value *ucontrol)
1784{
1785 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001786 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001787 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001788
1789 reg_val = snd_soc_read(widget->codec, e->reg);
1790 val = (reg_val >> e->shift_l) & e->mask;
1791 for (mux = 0; mux < e->max; mux++) {
1792 if (val == e->values[mux])
1793 break;
1794 }
1795 ucontrol->value.enumerated.item[0] = mux;
1796 if (e->shift_l != e->shift_r) {
1797 val = (reg_val >> e->shift_r) & e->mask;
1798 for (mux = 0; mux < e->max; mux++) {
1799 if (val == e->values[mux])
1800 break;
1801 }
1802 ucontrol->value.enumerated.item[1] = mux;
1803 }
1804
1805 return 0;
1806}
1807EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1808
1809/**
1810 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1811 * callback
1812 * @kcontrol: mixer control
1813 * @ucontrol: control element information
1814 *
1815 * Callback to set the value of a dapm semi enumerated double mixer control.
1816 *
1817 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1818 * used for handling bitfield coded enumeration for example.
1819 *
1820 * Returns 0 for success.
1821 */
1822int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1823 struct snd_ctl_elem_value *ucontrol)
1824{
1825 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001826 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001827 unsigned int val, mux;
1828 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001829 int ret = 0;
1830
1831 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1832 return -EINVAL;
1833 mux = ucontrol->value.enumerated.item[0];
1834 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1835 mask = e->mask << e->shift_l;
1836 if (e->shift_l != e->shift_r) {
1837 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1838 return -EINVAL;
1839 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1840 mask |= e->mask << e->shift_r;
1841 }
1842
1843 mutex_lock(&widget->codec->mutex);
1844 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001845 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001846 if (widget->event) {
1847 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1848 ret = widget->event(widget,
1849 kcontrol, SND_SOC_DAPM_PRE_REG);
1850 if (ret < 0)
1851 goto out;
1852 }
1853 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1854 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1855 ret = widget->event(widget,
1856 kcontrol, SND_SOC_DAPM_POST_REG);
1857 } else
1858 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1859
1860out:
1861 mutex_unlock(&widget->codec->mutex);
1862 return ret;
1863}
1864EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1865
1866/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001867 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1868 *
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1871 *
1872 * Callback to provide information about a pin switch control.
1873 */
1874int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1875 struct snd_ctl_elem_info *uinfo)
1876{
1877 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1878 uinfo->count = 1;
1879 uinfo->value.integer.min = 0;
1880 uinfo->value.integer.max = 1;
1881
1882 return 0;
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1885
1886/**
1887 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1888 *
1889 * @kcontrol: mixer control
1890 * @ucontrol: Value
1891 */
1892int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_value *ucontrol)
1894{
1895 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1896 const char *pin = (const char *)kcontrol->private_value;
1897
1898 mutex_lock(&codec->mutex);
1899
1900 ucontrol->value.integer.value[0] =
1901 snd_soc_dapm_get_pin_status(codec, pin);
1902
1903 mutex_unlock(&codec->mutex);
1904
1905 return 0;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1908
1909/**
1910 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1911 *
1912 * @kcontrol: mixer control
1913 * @ucontrol: Value
1914 */
1915int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1916 struct snd_ctl_elem_value *ucontrol)
1917{
1918 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1919 const char *pin = (const char *)kcontrol->private_value;
1920
1921 mutex_lock(&codec->mutex);
1922
1923 if (ucontrol->value.integer.value[0])
1924 snd_soc_dapm_enable_pin(codec, pin);
1925 else
1926 snd_soc_dapm_disable_pin(codec, pin);
1927
1928 snd_soc_dapm_sync(codec);
1929
1930 mutex_unlock(&codec->mutex);
1931
1932 return 0;
1933}
1934EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1935
1936/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937 * snd_soc_dapm_new_control - create new dapm control
1938 * @codec: audio codec
1939 * @widget: widget template
1940 *
1941 * Creates a new dapm control based upon the template.
1942 *
1943 * Returns 0 for success else error.
1944 */
1945int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1946 const struct snd_soc_dapm_widget *widget)
1947{
1948 struct snd_soc_dapm_widget *w;
1949
1950 if ((w = dapm_cnew_widget(widget)) == NULL)
1951 return -ENOMEM;
1952
1953 w->codec = codec;
1954 INIT_LIST_HEAD(&w->sources);
1955 INIT_LIST_HEAD(&w->sinks);
1956 INIT_LIST_HEAD(&w->list);
1957 list_add(&w->list, &codec->dapm_widgets);
1958
1959 /* machine layer set ups unconnected pins and insertions */
1960 w->connected = 1;
1961 return 0;
1962}
1963EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1964
1965/**
Mark Brown4ba13272008-05-13 14:51:19 +02001966 * snd_soc_dapm_new_controls - create new dapm controls
1967 * @codec: audio codec
1968 * @widget: widget array
1969 * @num: number of widgets
1970 *
1971 * Creates new DAPM controls based upon the templates.
1972 *
1973 * Returns 0 for success else error.
1974 */
1975int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1976 const struct snd_soc_dapm_widget *widget,
1977 int num)
1978{
1979 int i, ret;
1980
1981 for (i = 0; i < num; i++) {
1982 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001983 if (ret < 0) {
1984 printk(KERN_ERR
1985 "ASoC: Failed to create DAPM control %s: %d\n",
1986 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001987 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001988 }
Mark Brown4ba13272008-05-13 14:51:19 +02001989 widget++;
1990 }
1991 return 0;
1992}
1993EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1994
1995
1996/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1998 * @codec: audio codec
1999 * @stream: stream name
2000 * @event: stream event
2001 *
2002 * Sends a stream event to the dapm core. The core then makes any
2003 * necessary widget power changes.
2004 *
2005 * Returns 0 for success else error.
2006 */
2007int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2008 char *stream, int event)
2009{
2010 struct snd_soc_dapm_widget *w;
2011
Seth Forshee11da21a2007-02-02 17:14:19 +01002012 if (stream == NULL)
2013 return 0;
2014
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 mutex_lock(&codec->mutex);
2016 list_for_each_entry(w, &codec->dapm_widgets, list)
2017 {
2018 if (!w->sname)
2019 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002020 pr_debug("widget %s\n %s stream %s event %d\n",
2021 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 if (strstr(w->sname, stream)) {
2023 switch(event) {
2024 case SND_SOC_DAPM_STREAM_START:
2025 w->active = 1;
2026 break;
2027 case SND_SOC_DAPM_STREAM_STOP:
2028 w->active = 0;
2029 break;
2030 case SND_SOC_DAPM_STREAM_SUSPEND:
2031 if (w->active)
2032 w->suspend = 1;
2033 w->active = 0;
2034 break;
2035 case SND_SOC_DAPM_STREAM_RESUME:
2036 if (w->suspend) {
2037 w->active = 1;
2038 w->suspend = 0;
2039 }
2040 break;
2041 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2042 break;
2043 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2044 break;
2045 }
2046 }
2047 }
2048 mutex_unlock(&codec->mutex);
2049
2050 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08002051 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052 return 0;
2053}
2054EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2055
2056/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002057 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002058 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002059 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060 *
Mark Brown74b8f952009-06-06 11:26:15 +01002061 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002062 * a valid audio route and active audio stream.
2063 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2064 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 */
Mark Brown16499232009-01-07 18:25:13 +00002066int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067{
Liam Girdwooda5302182008-07-07 13:35:17 +01002068 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069}
Liam Girdwooda5302182008-07-07 13:35:17 +01002070EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071
2072/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002073 * snd_soc_dapm_disable_pin - disable pin.
2074 * @codec: SoC codec
2075 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002076 *
Mark Brown74b8f952009-06-06 11:26:15 +01002077 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2079 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002080 */
Mark Brown16499232009-01-07 18:25:13 +00002081int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002082{
2083 return snd_soc_dapm_set_pin(codec, pin, 0);
2084}
2085EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2086
2087/**
Mark Brown5817b522008-09-24 11:23:11 +01002088 * snd_soc_dapm_nc_pin - permanently disable pin.
2089 * @codec: SoC codec
2090 * @pin: pin name
2091 *
2092 * Marks the specified pin as being not connected, disabling it along
2093 * any parent or child widgets. At present this is identical to
2094 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2095 * additional things such as disabling controls which only affect
2096 * paths through the pin.
2097 *
2098 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2099 * do any widget power switching.
2100 */
Mark Brown16499232009-01-07 18:25:13 +00002101int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002102{
2103 return snd_soc_dapm_set_pin(codec, pin, 0);
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2106
2107/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002108 * snd_soc_dapm_get_pin_status - get audio pin status
2109 * @codec: audio codec
2110 * @pin: audio signal pin endpoint (or start point)
2111 *
2112 * Get audio pin status - connected or disconnected.
2113 *
2114 * Returns 1 for connected otherwise 0.
2115 */
Mark Brown16499232009-01-07 18:25:13 +00002116int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002117{
2118 struct snd_soc_dapm_widget *w;
2119
2120 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002121 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002122 return w->connected;
2123 }
2124
2125 return 0;
2126}
Liam Girdwooda5302182008-07-07 13:35:17 +01002127EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002128
2129/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130 * snd_soc_dapm_free - free dapm resources
2131 * @socdev: SoC device
2132 *
2133 * Free all dapm widgets and resources.
2134 */
2135void snd_soc_dapm_free(struct snd_soc_device *socdev)
2136{
Mark Brown6627a652009-01-23 22:55:23 +00002137 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138
2139 snd_soc_dapm_sys_remove(socdev->dev);
2140 dapm_free_widgets(codec);
2141}
2142EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2143
Mark Brown51737472009-06-22 13:16:51 +01002144/*
2145 * snd_soc_dapm_shutdown - callback for system shutdown
2146 */
2147void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2148{
2149 struct snd_soc_codec *codec = socdev->card->codec;
2150 struct snd_soc_dapm_widget *w;
2151 LIST_HEAD(down_list);
2152 int powerdown = 0;
2153
2154 list_for_each_entry(w, &codec->dapm_widgets, list) {
2155 if (w->power) {
2156 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002157 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002158 powerdown = 1;
2159 }
2160 }
2161
2162 /* If there were no widgets to power down we're already in
2163 * standby.
2164 */
2165 if (powerdown) {
2166 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2167 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2168 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2169 }
2170
2171 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2172}
2173
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002175MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2177MODULE_LICENSE("GPL");