blob: 7b74547b56fac73a8e5dd2fecb27a5640e4b8c58 [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,
59 [snd_soc_dapm_mic] = 3,
60 [snd_soc_dapm_mux] = 4,
Mark Browne3d4dab2009-06-07 13:08:45 +010061 [snd_soc_dapm_value_mux] = 4,
62 [snd_soc_dapm_dac] = 5,
63 [snd_soc_dapm_mixer] = 6,
64 [snd_soc_dapm_mixer_named_ctl] = 6,
65 [snd_soc_dapm_pga] = 7,
66 [snd_soc_dapm_adc] = 8,
67 [snd_soc_dapm_hp] = 9,
Mark Brown1ca04062009-08-17 16:26:59 +010068 [snd_soc_dapm_spk] = 9,
69 [snd_soc_dapm_post] = 10,
Richard Purdie2b97eab2006-10-06 18:32:18 +020070};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000071
Richard Purdie2b97eab2006-10-06 18:32:18 +020072static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_adc] = 1,
75 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010076 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010077 [snd_soc_dapm_pga] = 4,
78 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010079 [snd_soc_dapm_mixer] = 5,
80 [snd_soc_dapm_dac] = 6,
81 [snd_soc_dapm_mic] = 7,
82 [snd_soc_dapm_micbias] = 8,
83 [snd_soc_dapm_mux] = 9,
84 [snd_soc_dapm_value_mux] = 9,
85 [snd_soc_dapm_supply] = 10,
86 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020087};
88
Troy Kisky12ef1932008-10-13 17:42:14 -070089static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010090{
91 if (pop_time)
92 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
93}
94
Troy Kisky12ef1932008-10-13 17:42:14 -070095static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010096{
97 va_list args;
98
99 va_start(args, fmt);
100
101 if (pop_time) {
102 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700103 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100104 }
105
106 va_end(args);
107}
108
Richard Purdie2b97eab2006-10-06 18:32:18 +0200109/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100110static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200111 const struct snd_soc_dapm_widget *_widget)
112{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100113 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200114}
115
Mark Brown452c5ea2009-05-17 21:41:23 +0100116/**
117 * snd_soc_dapm_set_bias_level - set the bias level for the system
118 * @socdev: audio device
119 * @level: level to configure
120 *
121 * Configure the bias (power) levels for the SoC audio device.
122 *
123 * Returns 0 for success else error.
124 */
125static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
126 enum snd_soc_bias_level level)
127{
128 struct snd_soc_card *card = socdev->card;
129 struct snd_soc_codec *codec = socdev->card->codec;
130 int ret = 0;
131
Mark Brownf83fba82009-05-18 15:44:43 +0100132 switch (level) {
133 case SND_SOC_BIAS_ON:
134 dev_dbg(socdev->dev, "Setting full bias\n");
135 break;
136 case SND_SOC_BIAS_PREPARE:
137 dev_dbg(socdev->dev, "Setting bias prepare\n");
138 break;
139 case SND_SOC_BIAS_STANDBY:
140 dev_dbg(socdev->dev, "Setting standby bias\n");
141 break;
142 case SND_SOC_BIAS_OFF:
143 dev_dbg(socdev->dev, "Setting bias off\n");
144 break;
145 default:
146 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
147 return -EINVAL;
148 }
149
Mark Brown452c5ea2009-05-17 21:41:23 +0100150 if (card->set_bias_level)
151 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100152 if (ret == 0) {
153 if (codec->set_bias_level)
154 ret = codec->set_bias_level(codec, level);
155 else
156 codec->bias_level = level;
157 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100158
159 return ret;
160}
161
Richard Purdie2b97eab2006-10-06 18:32:18 +0200162/* set up initial codec paths */
163static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
164 struct snd_soc_dapm_path *p, int i)
165{
166 switch (w->id) {
167 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000168 case snd_soc_dapm_mixer:
169 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200170 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100171 struct soc_mixer_control *mc = (struct soc_mixer_control *)
172 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400173 unsigned int reg = mc->reg;
174 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100175 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400176 unsigned int mask = (1 << fls(max)) - 1;
177 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200178
179 val = snd_soc_read(w->codec, reg);
180 val = (val >> shift) & mask;
181
182 if ((invert && !val) || (!invert && val))
183 p->connect = 1;
184 else
185 p->connect = 0;
186 }
187 break;
188 case snd_soc_dapm_mux: {
189 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
190 int val, item, bitmask;
191
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100192 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200193 ;
194 val = snd_soc_read(w->codec, e->reg);
195 item = (val >> e->shift_l) & (bitmask - 1);
196
197 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100198 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200199 if (!(strcmp(p->name, e->texts[i])) && item == i)
200 p->connect = 1;
201 }
202 }
203 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200204 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200205 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200206 w->kcontrols[i].private_value;
207 int val, item;
208
209 val = snd_soc_read(w->codec, e->reg);
210 val = (val >> e->shift_l) & e->mask;
211 for (item = 0; item < e->max; item++) {
212 if (val == e->values[item])
213 break;
214 }
215
216 p->connect = 0;
217 for (i = 0; i < e->max; i++) {
218 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 p->connect = 1;
220 }
221 }
222 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200223 /* does not effect routing - always connected */
224 case snd_soc_dapm_pga:
225 case snd_soc_dapm_output:
226 case snd_soc_dapm_adc:
227 case snd_soc_dapm_input:
228 case snd_soc_dapm_dac:
229 case snd_soc_dapm_micbias:
230 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100231 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200232 p->connect = 1;
233 break;
234 /* does effect routing - dynamically connected */
235 case snd_soc_dapm_hp:
236 case snd_soc_dapm_mic:
237 case snd_soc_dapm_spk:
238 case snd_soc_dapm_line:
239 case snd_soc_dapm_pre:
240 case snd_soc_dapm_post:
241 p->connect = 0;
242 break;
243 }
244}
245
Mark Brown74b8f952009-06-06 11:26:15 +0100246/* connect mux widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200247static int dapm_connect_mux(struct snd_soc_codec *codec,
248 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
249 struct snd_soc_dapm_path *path, const char *control_name,
250 const struct snd_kcontrol_new *kcontrol)
251{
252 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
253 int i;
254
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100255 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200256 if (!(strcmp(control_name, e->texts[i]))) {
257 list_add(&path->list, &codec->dapm_paths);
258 list_add(&path->list_sink, &dest->sources);
259 list_add(&path->list_source, &src->sinks);
260 path->name = (char*)e->texts[i];
261 dapm_set_path_status(dest, path, 0);
262 return 0;
263 }
264 }
265
266 return -ENODEV;
267}
268
Mark Brown74b8f952009-06-06 11:26:15 +0100269/* connect mixer widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200270static int dapm_connect_mixer(struct snd_soc_codec *codec,
271 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
272 struct snd_soc_dapm_path *path, const char *control_name)
273{
274 int i;
275
276 /* search for mixer kcontrol */
277 for (i = 0; i < dest->num_kcontrols; i++) {
278 if (!strcmp(control_name, dest->kcontrols[i].name)) {
279 list_add(&path->list, &codec->dapm_paths);
280 list_add(&path->list_sink, &dest->sources);
281 list_add(&path->list_source, &src->sinks);
282 path->name = dest->kcontrols[i].name;
283 dapm_set_path_status(dest, path, i);
284 return 0;
285 }
286 }
287 return -ENODEV;
288}
289
290/* update dapm codec register bits */
291static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
292{
293 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300294 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295 struct snd_soc_codec *codec = widget->codec;
296
297 /* check for valid widgets */
298 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
299 widget->id == snd_soc_dapm_output ||
300 widget->id == snd_soc_dapm_hp ||
301 widget->id == snd_soc_dapm_mic ||
302 widget->id == snd_soc_dapm_line ||
303 widget->id == snd_soc_dapm_spk)
304 return 0;
305
306 power = widget->power;
307 if (widget->invert)
308 power = (power ? 0:1);
309
310 old = snd_soc_read(codec, widget->reg);
311 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
312
313 change = old != new;
314 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700315 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
316 widget->name, widget->power ? "on" : "off",
317 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200318 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700319 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200320 }
Mark Brownc1286b82008-07-07 19:26:03 +0100321 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
322 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200323 return change;
324}
325
326/* ramps the volume up or down to minimise pops before or after a
327 * DAPM power event */
328static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
329{
330 const struct snd_kcontrol_new *k = widget->kcontrols;
331
332 if (widget->muted && !power)
333 return 0;
334 if (!widget->muted && power)
335 return 0;
336
337 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100338 struct soc_mixer_control *mc =
339 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400340 unsigned int reg = mc->reg;
341 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100342 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400343 unsigned int mask = (1 << fls(max)) - 1;
344 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200345
346 if (power) {
347 int i;
348 /* power up has happended, increase volume to last level */
349 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100350 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 snd_soc_update_bits(widget->codec, reg, mask, i);
352 } else {
353 for (i = 0; i < widget->saved_value; i++)
354 snd_soc_update_bits(widget->codec, reg, mask, i);
355 }
356 widget->muted = 0;
357 } else {
358 /* power down is about to occur, decrease volume to mute */
359 int val = snd_soc_read(widget->codec, reg);
360 int i = widget->saved_value = (val >> shift) & mask;
361 if (invert) {
362 for (; i < mask; i++)
363 snd_soc_update_bits(widget->codec, reg, mask, i);
364 } else {
365 for (; i > 0; i--)
366 snd_soc_update_bits(widget->codec, reg, mask, i);
367 }
368 widget->muted = 1;
369 }
370 }
371 return 0;
372}
373
374/* create new dapm mixer control */
375static int dapm_new_mixer(struct snd_soc_codec *codec,
376 struct snd_soc_dapm_widget *w)
377{
378 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000379 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200380 struct snd_soc_dapm_path *path;
381
382 /* add kcontrol */
383 for (i = 0; i < w->num_kcontrols; i++) {
384
385 /* match name */
386 list_for_each_entry(path, &w->sources, list_sink) {
387
388 /* mixer/mux paths name must match control name */
389 if (path->name != (char*)w->kcontrols[i].name)
390 continue;
391
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000392 /* add dapm control with long name.
393 * for dapm_mixer this is the concatenation of the
394 * mixer and kcontrol name.
395 * for dapm_mixer_named_ctl this is simply the
396 * kcontrol name.
397 */
398 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000399 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000400 name_len += 1 + strlen(w->name);
401
Mark Brown219b93f2008-10-28 13:02:31 +0000402 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000403
Richard Purdie2b97eab2006-10-06 18:32:18 +0200404 if (path->long_name == NULL)
405 return -ENOMEM;
406
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000407 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000408 default:
409 snprintf(path->long_name, name_len, "%s %s",
410 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000411 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000412 case snd_soc_dapm_mixer_named_ctl:
413 snprintf(path->long_name, name_len, "%s",
414 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000415 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000416 }
417
Mark Brown219b93f2008-10-28 13:02:31 +0000418 path->long_name[name_len - 1] = '\0';
419
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
421 path->long_name);
422 ret = snd_ctl_add(codec->card, path->kcontrol);
423 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100424 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
425 path->long_name,
426 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427 kfree(path->long_name);
428 path->long_name = NULL;
429 return ret;
430 }
431 }
432 }
433 return ret;
434}
435
436/* create new dapm mux control */
437static int dapm_new_mux(struct snd_soc_codec *codec,
438 struct snd_soc_dapm_widget *w)
439{
440 struct snd_soc_dapm_path *path = NULL;
441 struct snd_kcontrol *kcontrol;
442 int ret = 0;
443
444 if (!w->num_kcontrols) {
445 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
446 return -EINVAL;
447 }
448
449 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
450 ret = snd_ctl_add(codec->card, kcontrol);
451 if (ret < 0)
452 goto err;
453
454 list_for_each_entry(path, &w->sources, list_sink)
455 path->kcontrol = kcontrol;
456
457 return ret;
458
459err:
460 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
461 return ret;
462}
463
464/* create new dapm volume control */
465static int dapm_new_pga(struct snd_soc_codec *codec,
466 struct snd_soc_dapm_widget *w)
467{
468 struct snd_kcontrol *kcontrol;
469 int ret = 0;
470
471 if (!w->num_kcontrols)
472 return -EINVAL;
473
474 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
475 ret = snd_ctl_add(codec->card, kcontrol);
476 if (ret < 0) {
477 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
478 return ret;
479 }
480
481 return ret;
482}
483
484/* reset 'walked' bit for each dapm path */
485static inline void dapm_clear_walk(struct snd_soc_codec *codec)
486{
487 struct snd_soc_dapm_path *p;
488
489 list_for_each_entry(p, &codec->dapm_paths, list)
490 p->walked = 0;
491}
492
493/*
494 * Recursively check for a completed path to an active or physically connected
495 * output widget. Returns number of complete paths.
496 */
497static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
498{
499 struct snd_soc_dapm_path *path;
500 int con = 0;
501
Mark Brown246d0a12009-04-22 18:24:55 +0100502 if (widget->id == snd_soc_dapm_supply)
503 return 0;
504
Richard Purdie2b97eab2006-10-06 18:32:18 +0200505 if (widget->id == snd_soc_dapm_adc && widget->active)
506 return 1;
507
508 if (widget->connected) {
509 /* connected pin ? */
510 if (widget->id == snd_soc_dapm_output && !widget->ext)
511 return 1;
512
513 /* connected jack or spk ? */
514 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
515 widget->id == snd_soc_dapm_line)
516 return 1;
517 }
518
519 list_for_each_entry(path, &widget->sinks, list_source) {
520 if (path->walked)
521 continue;
522
523 if (path->sink && path->connect) {
524 path->walked = 1;
525 con += is_connected_output_ep(path->sink);
526 }
527 }
528
529 return con;
530}
531
532/*
533 * Recursively check for a completed path to an active or physically connected
534 * input widget. Returns number of complete paths.
535 */
536static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
537{
538 struct snd_soc_dapm_path *path;
539 int con = 0;
540
Mark Brown246d0a12009-04-22 18:24:55 +0100541 if (widget->id == snd_soc_dapm_supply)
542 return 0;
543
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544 /* active stream ? */
545 if (widget->id == snd_soc_dapm_dac && widget->active)
546 return 1;
547
548 if (widget->connected) {
549 /* connected pin ? */
550 if (widget->id == snd_soc_dapm_input && !widget->ext)
551 return 1;
552
553 /* connected VMID/Bias for lower pops */
554 if (widget->id == snd_soc_dapm_vmid)
555 return 1;
556
557 /* connected jack ? */
558 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
559 return 1;
560 }
561
562 list_for_each_entry(path, &widget->sources, list_sink) {
563 if (path->walked)
564 continue;
565
566 if (path->source && path->connect) {
567 path->walked = 1;
568 con += is_connected_input_ep(path->source);
569 }
570 }
571
572 return con;
573}
574
575/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300576 * Handler for generic register modifier widget.
577 */
578int dapm_reg_event(struct snd_soc_dapm_widget *w,
579 struct snd_kcontrol *kcontrol, int event)
580{
581 unsigned int val;
582
583 if (SND_SOC_DAPM_EVENT_ON(event))
584 val = w->on_val;
585 else
586 val = w->off_val;
587
588 snd_soc_update_bits(w->codec, -(w->reg + 1),
589 w->mask << w->shift, val << w->shift);
590
591 return 0;
592}
Mark Brown11589412008-07-29 11:42:23 +0100593EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300594
Mark Brown025756e2009-04-13 11:09:18 +0100595/* Standard power change method, used to apply power changes to most
596 * widgets.
597 */
598static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
599{
600 int ret;
601
602 /* call any power change event handlers */
603 if (w->event)
604 pr_debug("power %s event for %s flags %x\n",
605 w->power ? "on" : "off",
606 w->name, w->event_flags);
607
608 /* power up pre event */
609 if (w->power && w->event &&
610 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
611 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
612 if (ret < 0)
613 return ret;
614 }
615
616 /* power down pre event */
617 if (!w->power && w->event &&
618 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
619 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
620 if (ret < 0)
621 return ret;
622 }
623
624 /* Lower PGA volume to reduce pops */
625 if (w->id == snd_soc_dapm_pga && !w->power)
626 dapm_set_pga(w, w->power);
627
628 dapm_update_bits(w);
629
630 /* Raise PGA volume to reduce pops */
631 if (w->id == snd_soc_dapm_pga && w->power)
632 dapm_set_pga(w, w->power);
633
634 /* power up post event */
635 if (w->power && w->event &&
636 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
637 ret = w->event(w,
638 NULL, SND_SOC_DAPM_POST_PMU);
639 if (ret < 0)
640 return ret;
641 }
642
643 /* power down post event */
644 if (!w->power && w->event &&
645 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
646 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
647 if (ret < 0)
648 return ret;
649 }
650
651 return 0;
652}
653
Mark Browncd0f2d42009-04-20 16:56:59 +0100654/* Generic check to see if a widget should be powered.
655 */
656static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
657{
658 int in, out;
659
660 in = is_connected_input_ep(w);
661 dapm_clear_walk(w->codec);
662 out = is_connected_output_ep(w);
663 dapm_clear_walk(w->codec);
664 return out != 0 && in != 0;
665}
666
Mark Brown6ea31b92009-04-20 17:15:41 +0100667/* Check to see if an ADC has power */
668static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
669{
670 int in;
671
672 if (w->active) {
673 in = is_connected_input_ep(w);
674 dapm_clear_walk(w->codec);
675 return in != 0;
676 } else {
677 return dapm_generic_check_power(w);
678 }
679}
680
681/* Check to see if a DAC has power */
682static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
683{
684 int out;
685
686 if (w->active) {
687 out = is_connected_output_ep(w);
688 dapm_clear_walk(w->codec);
689 return out != 0;
690 } else {
691 return dapm_generic_check_power(w);
692 }
693}
694
Mark Brown246d0a12009-04-22 18:24:55 +0100695/* Check to see if a power supply is needed */
696static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
697{
698 struct snd_soc_dapm_path *path;
699 int power = 0;
700
701 /* Check if one of our outputs is connected */
702 list_for_each_entry(path, &w->sinks, list_source) {
703 if (path->sink && path->sink->power_check &&
704 path->sink->power_check(path->sink)) {
705 power = 1;
706 break;
707 }
708 }
709
710 dapm_clear_walk(w->codec);
711
712 return power;
713}
714
Mark Brown38357ab2009-06-06 19:03:23 +0100715static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
716 struct snd_soc_dapm_widget *b,
717 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000718{
Mark Brown38357ab2009-06-06 19:03:23 +0100719 if (sort[a->id] != sort[b->id])
720 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100721 if (a->reg != b->reg)
722 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000723
Mark Brown38357ab2009-06-06 19:03:23 +0100724 return 0;
725}
Mark Brown42aa3412009-03-01 19:21:10 +0000726
Mark Brown38357ab2009-06-06 19:03:23 +0100727/* Insert a widget in order into a DAPM power sequence. */
728static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
729 struct list_head *list,
730 int sort[])
731{
732 struct snd_soc_dapm_widget *w;
733
734 list_for_each_entry(w, list, power_list)
735 if (dapm_seq_compare(new_widget, w, sort) < 0) {
736 list_add_tail(&new_widget->power_list, &w->power_list);
737 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000738 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100739
Mark Brown38357ab2009-06-06 19:03:23 +0100740 list_add_tail(&new_widget->power_list, list);
741}
Mark Brown42aa3412009-03-01 19:21:10 +0000742
Mark Brownb22ead22009-06-07 12:51:26 +0100743/* Apply the coalesced changes from a DAPM sequence */
744static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
745 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100746{
747 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100748 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100749 unsigned int value = 0;
750 unsigned int mask = 0;
751 unsigned int cur_mask;
752
753 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
754 power_list)->reg;
755
756 list_for_each_entry(w, pending, power_list) {
757 cur_mask = 1 << w->shift;
758 BUG_ON(reg != w->reg);
759
760 if (w->invert)
761 power = !w->power;
762 else
763 power = w->power;
764
765 mask |= cur_mask;
766 if (power)
767 value |= cur_mask;
768
769 pop_dbg(codec->pop_time,
770 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
771 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100772
773 /* power up pre event */
774 if (w->power && w->event &&
775 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
776 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
777 w->name);
778 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
779 if (ret < 0)
780 pr_err("%s: pre event failed: %d\n",
781 w->name, ret);
782 }
783
784 /* power down pre event */
785 if (!w->power && w->event &&
786 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
787 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
788 w->name);
789 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
790 if (ret < 0)
791 pr_err("%s: pre event failed: %d\n",
792 w->name, ret);
793 }
Mark Brown4f1c1922009-06-07 13:37:44 +0100794
795 /* Lower PGA volume to reduce pops */
796 if (w->id == snd_soc_dapm_pga && !w->power)
797 dapm_set_pga(w, w->power);
Mark Brownb22ead22009-06-07 12:51:26 +0100798 }
799
Mark Brown81628102009-06-07 13:21:24 +0100800 if (reg >= 0) {
801 pop_dbg(codec->pop_time,
802 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
803 value, mask, reg, codec->pop_time);
804 pop_wait(codec->pop_time);
805 snd_soc_update_bits(codec, reg, mask, value);
806 }
807
808 list_for_each_entry(w, pending, power_list) {
Mark Brown4f1c1922009-06-07 13:37:44 +0100809 /* Raise PGA volume to reduce pops */
810 if (w->id == snd_soc_dapm_pga && w->power)
811 dapm_set_pga(w, w->power);
812
Mark Brown81628102009-06-07 13:21:24 +0100813 /* power up post event */
814 if (w->power && w->event &&
815 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
816 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
817 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000818 ret = w->event(w,
819 NULL, SND_SOC_DAPM_POST_PMU);
820 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100821 pr_err("%s: post event failed: %d\n",
822 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000823 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100824
Mark Brown81628102009-06-07 13:21:24 +0100825 /* power down post event */
826 if (!w->power && w->event &&
827 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
828 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
829 w->name);
830 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
831 if (ret < 0)
832 pr_err("%s: post event failed: %d\n",
833 w->name, ret);
834 }
Mark Brown42aa3412009-03-01 19:21:10 +0000835 }
Mark Brown42aa3412009-03-01 19:21:10 +0000836}
837
Mark Brownb22ead22009-06-07 12:51:26 +0100838/* Apply a DAPM power sequence.
839 *
840 * We walk over a pre-sorted list of widgets to apply power to. In
841 * order to minimise the number of writes to the device required
842 * multiple widgets will be updated in a single write where possible.
843 * Currently anything that requires more than a single write is not
844 * handled.
845 */
846static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
847 int event, int sort[])
848{
849 struct snd_soc_dapm_widget *w, *n;
850 LIST_HEAD(pending);
851 int cur_sort = -1;
852 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100853 int ret;
854
Mark Brownb22ead22009-06-07 12:51:26 +0100855 list_for_each_entry_safe(w, n, list, power_list) {
856 ret = 0;
857
858 /* Do we need to apply any queued changes? */
859 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
860 if (!list_empty(&pending))
861 dapm_seq_run_coalesced(codec, &pending);
862
863 INIT_LIST_HEAD(&pending);
864 cur_sort = -1;
865 cur_reg = SND_SOC_NOPM;
866 }
867
Mark Brown163cac02009-06-07 10:12:52 +0100868 switch (w->id) {
869 case snd_soc_dapm_pre:
870 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100871 list_for_each_entry_safe_continue(w, n, list,
872 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100873
Mark Brownb22ead22009-06-07 12:51:26 +0100874 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100875 ret = w->event(w,
876 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100877 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100878 ret = w->event(w,
879 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100880 break;
881
882 case snd_soc_dapm_post:
883 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100884 list_for_each_entry_safe_continue(w, n, list,
885 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100886
Mark Brownb22ead22009-06-07 12:51:26 +0100887 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100888 ret = w->event(w,
889 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100890 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100891 ret = w->event(w,
892 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100893 break;
894
895 case snd_soc_dapm_input:
896 case snd_soc_dapm_output:
897 case snd_soc_dapm_hp:
898 case snd_soc_dapm_mic:
899 case snd_soc_dapm_line:
900 case snd_soc_dapm_spk:
901 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100902 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100903 break;
904
905 default:
Mark Brown81628102009-06-07 13:21:24 +0100906 /* Queue it up for application */
907 cur_sort = sort[w->id];
908 cur_reg = w->reg;
909 list_move(&w->power_list, &pending);
910 break;
Mark Brown163cac02009-06-07 10:12:52 +0100911 }
Mark Brownb22ead22009-06-07 12:51:26 +0100912
913 if (ret < 0)
914 pr_err("Failed to apply widget power: %d\n",
915 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100916 }
Mark Brownb22ead22009-06-07 12:51:26 +0100917
918 if (!list_empty(&pending))
919 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100920}
921
Mark Brown42aa3412009-03-01 19:21:10 +0000922/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923 * Scan each dapm widget for complete audio path.
924 * A complete path is a route that has valid endpoints i.e.:-
925 *
926 * o DAC to output pin.
927 * o Input Pin to ADC.
928 * o Input pin to Output pin (bypass, sidetone)
929 * o DAC to ADC (loopback).
930 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100931static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932{
Mark Brown452c5ea2009-05-17 21:41:23 +0100933 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100935 LIST_HEAD(up_list);
936 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100937 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100938 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100939 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200940
Mark Brown6d3ddc82009-05-16 17:47:29 +0100941 /* Check which widgets we need to power and store them in
942 * lists indicating if they should be powered up or down.
943 */
944 list_for_each_entry(w, &codec->dapm_widgets, list) {
945 switch (w->id) {
946 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100947 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100948 break;
949 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100950 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100951 break;
952
953 default:
954 if (!w->power_check)
955 continue;
956
957 power = w->power_check(w);
Mark Brown452c5ea2009-05-17 21:41:23 +0100958 if (power)
959 sys_power = 1;
960
Mark Brown6d3ddc82009-05-16 17:47:29 +0100961 if (w->power == power)
962 continue;
963
964 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100965 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100966 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100967 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100968
969 w->power = power;
970 break;
971 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200972 }
973
Mark Brownb14b76a2009-08-17 11:55:38 +0100974 /* If there are no DAPM widgets then try to figure out power from the
975 * event type.
976 */
977 if (list_empty(&codec->dapm_widgets)) {
978 switch (event) {
979 case SND_SOC_DAPM_STREAM_START:
980 case SND_SOC_DAPM_STREAM_RESUME:
981 sys_power = 1;
982 break;
983 case SND_SOC_DAPM_STREAM_NOP:
984 sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
985 default:
986 break;
987 }
988 }
989
Mark Brown452c5ea2009-05-17 21:41:23 +0100990 /* If we're changing to all on or all off then prepare */
991 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
992 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
993 ret = snd_soc_dapm_set_bias_level(socdev,
994 SND_SOC_BIAS_PREPARE);
995 if (ret != 0)
996 pr_err("Failed to prepare bias: %d\n", ret);
997 }
998
Mark Brown6d3ddc82009-05-16 17:47:29 +0100999 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001000 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001001
1002 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001003 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001004
Mark Brown452c5ea2009-05-17 21:41:23 +01001005 /* If we just powered the last thing off drop to standby bias */
1006 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1007 ret = snd_soc_dapm_set_bias_level(socdev,
1008 SND_SOC_BIAS_STANDBY);
1009 if (ret != 0)
1010 pr_err("Failed to apply standby bias: %d\n", ret);
1011 }
1012
1013 /* If we just powered up then move to active bias */
1014 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1015 ret = snd_soc_dapm_set_bias_level(socdev,
1016 SND_SOC_BIAS_ON);
1017 if (ret != 0)
1018 pr_err("Failed to apply active bias: %d\n", ret);
1019 }
1020
Mark Browncb507e72009-07-08 18:54:57 +01001021 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1022 codec->pop_time);
1023
Mark Brown42aa3412009-03-01 19:21:10 +00001024 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001025}
1026
Mark Brownc1286b82008-07-07 19:26:03 +01001027#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
1029{
1030 struct snd_soc_dapm_widget *w;
1031 struct snd_soc_dapm_path *p = NULL;
1032 int in, out;
1033
1034 printk("DAPM %s %s\n", codec->name, action);
1035
1036 list_for_each_entry(w, &codec->dapm_widgets, list) {
1037
1038 /* only display widgets that effect routing */
1039 switch (w->id) {
1040 case snd_soc_dapm_pre:
1041 case snd_soc_dapm_post:
1042 case snd_soc_dapm_vmid:
1043 continue;
1044 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001045 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001046 case snd_soc_dapm_output:
1047 case snd_soc_dapm_input:
1048 case snd_soc_dapm_switch:
1049 case snd_soc_dapm_hp:
1050 case snd_soc_dapm_mic:
1051 case snd_soc_dapm_spk:
1052 case snd_soc_dapm_line:
1053 case snd_soc_dapm_micbias:
1054 case snd_soc_dapm_dac:
1055 case snd_soc_dapm_adc:
1056 case snd_soc_dapm_pga:
1057 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001058 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001059 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001060 if (w->name) {
1061 in = is_connected_input_ep(w);
1062 dapm_clear_walk(w->codec);
1063 out = is_connected_output_ep(w);
1064 dapm_clear_walk(w->codec);
1065 printk("%s: %s in %d out %d\n", w->name,
1066 w->power ? "On":"Off",in, out);
1067
1068 list_for_each_entry(p, &w->sources, list_sink) {
1069 if (p->connect)
1070 printk(" in %s %s\n", p->name ? p->name : "static",
1071 p->source->name);
1072 }
1073 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001074 if (p->connect)
1075 printk(" out %s %s\n", p->name ? p->name : "static",
1076 p->sink->name);
1077 }
1078 }
1079 break;
1080 }
1081 }
1082}
1083#endif
1084
Mark Brown79fb9382009-08-21 16:38:13 +01001085#ifdef CONFIG_DEBUG_FS
1086static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1087{
1088 file->private_data = inode->i_private;
1089 return 0;
1090}
1091
1092static ssize_t dapm_widget_power_read_file(struct file *file,
1093 char __user *user_buf,
1094 size_t count, loff_t *ppos)
1095{
1096 struct snd_soc_dapm_widget *w = file->private_data;
1097 char *buf;
1098 int in, out;
1099 ssize_t ret;
1100 struct snd_soc_dapm_path *p = NULL;
1101
1102 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1103 if (!buf)
1104 return -ENOMEM;
1105
1106 in = is_connected_input_ep(w);
1107 dapm_clear_walk(w->codec);
1108 out = is_connected_output_ep(w);
1109 dapm_clear_walk(w->codec);
1110
1111 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d\n",
1112 w->name, w->power ? "On" : "Off", in, out);
1113
1114 if (w->active && w->sname)
1115 ret += snprintf(buf, PAGE_SIZE - ret, " stream %s active\n",
1116 w->sname);
1117
1118 list_for_each_entry(p, &w->sources, list_sink) {
1119 if (p->connect)
1120 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1121 " in %s %s\n",
1122 p->name ? p->name : "static",
1123 p->source->name);
1124 }
1125 list_for_each_entry(p, &w->sinks, list_source) {
1126 if (p->connect)
1127 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1128 " out %s %s\n",
1129 p->name ? p->name : "static",
1130 p->sink->name);
1131 }
1132
1133 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1134
1135 kfree(buf);
1136 return ret;
1137}
1138
1139static const struct file_operations dapm_widget_power_fops = {
1140 .open = dapm_widget_power_open_file,
1141 .read = dapm_widget_power_read_file,
1142};
1143
1144void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1145{
1146 struct snd_soc_dapm_widget *w;
1147 struct dentry *d;
1148
1149 if (!codec->debugfs_dapm)
1150 return;
1151
1152 list_for_each_entry(w, &codec->dapm_widgets, list) {
1153 if (!w->name)
1154 continue;
1155
1156 d = debugfs_create_file(w->name, 0444,
1157 codec->debugfs_dapm, w,
1158 &dapm_widget_power_fops);
1159 if (!d)
1160 printk(KERN_WARNING
1161 "ASoC: Failed to create %s debugfs file\n",
1162 w->name);
1163 }
1164}
1165#else
1166void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1167{
1168}
1169#endif
1170
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001172static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1173 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001174 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175{
1176 struct snd_soc_dapm_path *path;
1177 int found = 0;
1178
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001179 if (widget->id != snd_soc_dapm_mux &&
1180 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001181 return -ENODEV;
1182
1183 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
1184 return 0;
1185
1186 /* find dapm widget path assoc with kcontrol */
1187 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1188 if (path->kcontrol != kcontrol)
1189 continue;
1190
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001191 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001192 continue;
1193
1194 found = 1;
1195 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001196 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001197 path->connect = 1; /* new connection */
1198 else
1199 path->connect = 0; /* old connection must be powered down */
1200 }
1201
Mark Brown3fccd8b2008-07-07 19:26:04 +01001202 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001203 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001204 dump_dapm(widget->codec, "mux power update");
1205 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206
1207 return 0;
1208}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209
Milan plzik1b075e32008-01-10 14:39:46 +01001210/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001211static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1212 struct snd_kcontrol *kcontrol, int reg,
1213 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214{
1215 struct snd_soc_dapm_path *path;
1216 int found = 0;
1217
Milan plzik1b075e32008-01-10 14:39:46 +01001218 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001219 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001220 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001221 return -ENODEV;
1222
1223 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
1224 return 0;
1225
1226 /* find dapm widget path assoc with kcontrol */
1227 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1228 if (path->kcontrol != kcontrol)
1229 continue;
1230
1231 /* found, now check type */
1232 found = 1;
1233 if (val)
1234 /* new connection */
1235 path->connect = invert ? 0:1;
1236 else
1237 /* old connection must be powered down */
1238 path->connect = invert ? 1:0;
1239 break;
1240 }
1241
Mark Brown3fccd8b2008-07-07 19:26:04 +01001242 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001243 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001244 dump_dapm(widget->codec, "mixer power update");
1245 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001246
1247 return 0;
1248}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249
1250/* show dapm widget status in sys fs */
1251static ssize_t dapm_widget_show(struct device *dev,
1252 struct device_attribute *attr, char *buf)
1253{
1254 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001255 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001256 struct snd_soc_dapm_widget *w;
1257 int count = 0;
1258 char *state = "not set";
1259
1260 list_for_each_entry(w, &codec->dapm_widgets, list) {
1261
1262 /* only display widgets that burnm power */
1263 switch (w->id) {
1264 case snd_soc_dapm_hp:
1265 case snd_soc_dapm_mic:
1266 case snd_soc_dapm_spk:
1267 case snd_soc_dapm_line:
1268 case snd_soc_dapm_micbias:
1269 case snd_soc_dapm_dac:
1270 case snd_soc_dapm_adc:
1271 case snd_soc_dapm_pga:
1272 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001273 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001274 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275 if (w->name)
1276 count += sprintf(buf + count, "%s: %s\n",
1277 w->name, w->power ? "On":"Off");
1278 break;
1279 default:
1280 break;
1281 }
1282 }
1283
Mark Brown0be98982008-05-19 12:31:28 +02001284 switch (codec->bias_level) {
1285 case SND_SOC_BIAS_ON:
1286 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287 break;
Mark Brown0be98982008-05-19 12:31:28 +02001288 case SND_SOC_BIAS_PREPARE:
1289 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001290 break;
Mark Brown0be98982008-05-19 12:31:28 +02001291 case SND_SOC_BIAS_STANDBY:
1292 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001293 break;
Mark Brown0be98982008-05-19 12:31:28 +02001294 case SND_SOC_BIAS_OFF:
1295 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001296 break;
1297 }
1298 count += sprintf(buf + count, "PM State: %s\n", state);
1299
1300 return count;
1301}
1302
1303static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1304
1305int snd_soc_dapm_sys_add(struct device *dev)
1306{
Troy Kisky12ef1932008-10-13 17:42:14 -07001307 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001308}
1309
1310static void snd_soc_dapm_sys_remove(struct device *dev)
1311{
Mark Brownaef90842009-05-16 17:53:16 +01001312 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313}
1314
1315/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001316static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001317{
1318 struct snd_soc_dapm_widget *w, *next_w;
1319 struct snd_soc_dapm_path *p, *next_p;
1320
1321 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1322 list_del(&w->list);
1323 kfree(w);
1324 }
1325
1326 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1327 list_del(&p->list);
1328 kfree(p->long_name);
1329 kfree(p);
1330 }
1331}
1332
Liam Girdwooda5302182008-07-07 13:35:17 +01001333static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001334 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001335{
1336 struct snd_soc_dapm_widget *w;
1337
1338 list_for_each_entry(w, &codec->dapm_widgets, list) {
1339 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001340 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001341 w->connected = status;
1342 return 0;
1343 }
1344 }
1345
Mark Brownc1286b82008-07-07 19:26:03 +01001346 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001347 return -EINVAL;
1348}
1349
Richard Purdie2b97eab2006-10-06 18:32:18 +02001350/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001351 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352 * @codec: audio codec
1353 *
1354 * Walks all dapm audio paths and powers widgets according to their
1355 * stream or path usage.
1356 *
1357 * Returns 0 for success.
1358 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001359int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001360{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001361 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1362 dump_dapm(codec, "sync");
1363 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001364}
Liam Girdwooda5302182008-07-07 13:35:17 +01001365EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001366
Mark Brown105f1c22008-05-13 14:52:19 +02001367static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1368 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001369{
1370 struct snd_soc_dapm_path *path;
1371 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1372 int ret = 0;
1373
1374 /* find src and dest widgets */
1375 list_for_each_entry(w, &codec->dapm_widgets, list) {
1376
1377 if (!wsink && !(strcmp(w->name, sink))) {
1378 wsink = w;
1379 continue;
1380 }
1381 if (!wsource && !(strcmp(w->name, source))) {
1382 wsource = w;
1383 }
1384 }
1385
1386 if (wsource == NULL || wsink == NULL)
1387 return -ENODEV;
1388
1389 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1390 if (!path)
1391 return -ENOMEM;
1392
1393 path->source = wsource;
1394 path->sink = wsink;
1395 INIT_LIST_HEAD(&path->list);
1396 INIT_LIST_HEAD(&path->list_source);
1397 INIT_LIST_HEAD(&path->list_sink);
1398
1399 /* check for external widgets */
1400 if (wsink->id == snd_soc_dapm_input) {
1401 if (wsource->id == snd_soc_dapm_micbias ||
1402 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001403 wsource->id == snd_soc_dapm_line ||
1404 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001405 wsink->ext = 1;
1406 }
1407 if (wsource->id == snd_soc_dapm_output) {
1408 if (wsink->id == snd_soc_dapm_spk ||
1409 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001410 wsink->id == snd_soc_dapm_line ||
1411 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001412 wsource->ext = 1;
1413 }
1414
1415 /* connect static paths */
1416 if (control == NULL) {
1417 list_add(&path->list, &codec->dapm_paths);
1418 list_add(&path->list_sink, &wsink->sources);
1419 list_add(&path->list_source, &wsource->sinks);
1420 path->connect = 1;
1421 return 0;
1422 }
1423
1424 /* connect dynamic paths */
1425 switch(wsink->id) {
1426 case snd_soc_dapm_adc:
1427 case snd_soc_dapm_dac:
1428 case snd_soc_dapm_pga:
1429 case snd_soc_dapm_input:
1430 case snd_soc_dapm_output:
1431 case snd_soc_dapm_micbias:
1432 case snd_soc_dapm_vmid:
1433 case snd_soc_dapm_pre:
1434 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001435 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001436 list_add(&path->list, &codec->dapm_paths);
1437 list_add(&path->list_sink, &wsink->sources);
1438 list_add(&path->list_source, &wsource->sinks);
1439 path->connect = 1;
1440 return 0;
1441 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001442 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1444 &wsink->kcontrols[0]);
1445 if (ret != 0)
1446 goto err;
1447 break;
1448 case snd_soc_dapm_switch:
1449 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001450 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001451 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1452 if (ret != 0)
1453 goto err;
1454 break;
1455 case snd_soc_dapm_hp:
1456 case snd_soc_dapm_mic:
1457 case snd_soc_dapm_line:
1458 case snd_soc_dapm_spk:
1459 list_add(&path->list, &codec->dapm_paths);
1460 list_add(&path->list_sink, &wsink->sources);
1461 list_add(&path->list_source, &wsource->sinks);
1462 path->connect = 0;
1463 return 0;
1464 }
1465 return 0;
1466
1467err:
1468 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1469 control, sink);
1470 kfree(path);
1471 return ret;
1472}
Mark Brown105f1c22008-05-13 14:52:19 +02001473
1474/**
Mark Brown105f1c22008-05-13 14:52:19 +02001475 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1476 * @codec: codec
1477 * @route: audio routes
1478 * @num: number of routes
1479 *
1480 * Connects 2 dapm widgets together via a named audio path. The sink is
1481 * the widget receiving the audio signal, whilst the source is the sender
1482 * of the audio signal.
1483 *
1484 * Returns 0 for success else error. On error all resources can be freed
1485 * with a call to snd_soc_card_free().
1486 */
1487int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1488 const struct snd_soc_dapm_route *route, int num)
1489{
1490 int i, ret;
1491
1492 for (i = 0; i < num; i++) {
1493 ret = snd_soc_dapm_add_route(codec, route->sink,
1494 route->control, route->source);
1495 if (ret < 0) {
1496 printk(KERN_ERR "Failed to add route %s->%s\n",
1497 route->source,
1498 route->sink);
1499 return ret;
1500 }
1501 route++;
1502 }
1503
1504 return 0;
1505}
1506EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1507
1508/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509 * snd_soc_dapm_new_widgets - add new dapm widgets
1510 * @codec: audio codec
1511 *
1512 * Checks the codec for any new dapm widgets and creates them if found.
1513 *
1514 * Returns 0 for success.
1515 */
1516int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1517{
1518 struct snd_soc_dapm_widget *w;
1519
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 list_for_each_entry(w, &codec->dapm_widgets, list)
1521 {
1522 if (w->new)
1523 continue;
1524
1525 switch(w->id) {
1526 case snd_soc_dapm_switch:
1527 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001528 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001529 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001530 dapm_new_mixer(codec, w);
1531 break;
1532 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001533 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001534 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001535 dapm_new_mux(codec, w);
1536 break;
1537 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001538 w->power_check = dapm_adc_check_power;
1539 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001540 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001541 w->power_check = dapm_dac_check_power;
1542 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001544 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 dapm_new_pga(codec, w);
1546 break;
1547 case snd_soc_dapm_input:
1548 case snd_soc_dapm_output:
1549 case snd_soc_dapm_micbias:
1550 case snd_soc_dapm_spk:
1551 case snd_soc_dapm_hp:
1552 case snd_soc_dapm_mic:
1553 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001554 w->power_check = dapm_generic_check_power;
1555 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001556 case snd_soc_dapm_supply:
1557 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 case snd_soc_dapm_vmid:
1559 case snd_soc_dapm_pre:
1560 case snd_soc_dapm_post:
1561 break;
1562 }
1563 w->new = 1;
1564 }
1565
1566 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 return 0;
1568}
1569EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1570
1571/**
1572 * snd_soc_dapm_get_volsw - dapm mixer get callback
1573 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001574 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 *
1576 * Callback to get the value of a dapm mixer control.
1577 *
1578 * Returns 0 for success.
1579 */
1580int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001584 struct soc_mixer_control *mc =
1585 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001586 unsigned int reg = mc->reg;
1587 unsigned int shift = mc->shift;
1588 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001589 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001590 unsigned int invert = mc->invert;
1591 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001592
1593 /* return the saved value if we are powered down */
1594 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1595 ucontrol->value.integer.value[0] = widget->saved_value;
1596 return 0;
1597 }
1598
1599 ucontrol->value.integer.value[0] =
1600 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1601 if (shift != rshift)
1602 ucontrol->value.integer.value[1] =
1603 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1604 if (invert) {
1605 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001606 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001607 if (shift != rshift)
1608 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001609 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610 }
1611
1612 return 0;
1613}
1614EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1615
1616/**
1617 * snd_soc_dapm_put_volsw - dapm mixer set callback
1618 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001619 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 *
1621 * Callback to set the value of a dapm mixer control.
1622 *
1623 * Returns 0 for success.
1624 */
1625int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1626 struct snd_ctl_elem_value *ucontrol)
1627{
1628 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001629 struct soc_mixer_control *mc =
1630 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001631 unsigned int reg = mc->reg;
1632 unsigned int shift = mc->shift;
1633 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001634 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001635 unsigned int mask = (1 << fls(max)) - 1;
1636 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001637 unsigned int val, val2, val_mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001638 int ret;
1639
1640 val = (ucontrol->value.integer.value[0] & mask);
1641
1642 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001643 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 val_mask = mask << shift;
1645 val = val << shift;
1646 if (shift != rshift) {
1647 val2 = (ucontrol->value.integer.value[1] & mask);
1648 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001649 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 val_mask |= mask << rshift;
1651 val |= val2 << rshift;
1652 }
1653
1654 mutex_lock(&widget->codec->mutex);
1655 widget->value = val;
1656
1657 /* save volume value if the widget is powered down */
1658 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1659 widget->saved_value = val;
1660 mutex_unlock(&widget->codec->mutex);
1661 return 1;
1662 }
1663
1664 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1665 if (widget->event) {
1666 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001667 ret = widget->event(widget, kcontrol,
1668 SND_SOC_DAPM_PRE_REG);
1669 if (ret < 0) {
1670 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001672 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001673 }
1674 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1675 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001676 ret = widget->event(widget, kcontrol,
1677 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001678 } else
1679 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1680
1681out:
1682 mutex_unlock(&widget->codec->mutex);
1683 return ret;
1684}
1685EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1686
1687/**
1688 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1689 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001690 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691 *
1692 * Callback to get the value of a dapm enumerated double mixer control.
1693 *
1694 * Returns 0 for success.
1695 */
1696int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1697 struct snd_ctl_elem_value *ucontrol)
1698{
1699 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1700 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001701 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001702
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001703 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 ;
1705 val = snd_soc_read(widget->codec, e->reg);
1706 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1707 if (e->shift_l != e->shift_r)
1708 ucontrol->value.enumerated.item[1] =
1709 (val >> e->shift_r) & (bitmask - 1);
1710
1711 return 0;
1712}
1713EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1714
1715/**
1716 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1717 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001718 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 *
1720 * Callback to set the value of a dapm enumerated double mixer control.
1721 *
1722 * Returns 0 for success.
1723 */
1724int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
1726{
1727 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1728 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001729 unsigned int val, mux;
1730 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001731 int ret = 0;
1732
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001733 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001735 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736 return -EINVAL;
1737 mux = ucontrol->value.enumerated.item[0];
1738 val = mux << e->shift_l;
1739 mask = (bitmask - 1) << e->shift_l;
1740 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001741 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001742 return -EINVAL;
1743 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1744 mask |= (bitmask - 1) << e->shift_r;
1745 }
1746
1747 mutex_lock(&widget->codec->mutex);
1748 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001749 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 if (widget->event) {
1751 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001752 ret = widget->event(widget,
1753 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001754 if (ret < 0)
1755 goto out;
1756 }
1757 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1758 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001759 ret = widget->event(widget,
1760 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761 } else
1762 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1763
1764out:
1765 mutex_unlock(&widget->codec->mutex);
1766 return ret;
1767}
1768EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1769
1770/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001771 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1772 * callback
1773 * @kcontrol: mixer control
1774 * @ucontrol: control element information
1775 *
1776 * Callback to get the value of a dapm semi enumerated double mixer control.
1777 *
1778 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1779 * used for handling bitfield coded enumeration for example.
1780 *
1781 * Returns 0 for success.
1782 */
1783int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_value *ucontrol)
1785{
1786 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001787 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001788 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001789
1790 reg_val = snd_soc_read(widget->codec, e->reg);
1791 val = (reg_val >> e->shift_l) & e->mask;
1792 for (mux = 0; mux < e->max; mux++) {
1793 if (val == e->values[mux])
1794 break;
1795 }
1796 ucontrol->value.enumerated.item[0] = mux;
1797 if (e->shift_l != e->shift_r) {
1798 val = (reg_val >> e->shift_r) & e->mask;
1799 for (mux = 0; mux < e->max; mux++) {
1800 if (val == e->values[mux])
1801 break;
1802 }
1803 ucontrol->value.enumerated.item[1] = mux;
1804 }
1805
1806 return 0;
1807}
1808EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1809
1810/**
1811 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1812 * callback
1813 * @kcontrol: mixer control
1814 * @ucontrol: control element information
1815 *
1816 * Callback to set the value of a dapm semi enumerated double mixer control.
1817 *
1818 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1819 * used for handling bitfield coded enumeration for example.
1820 *
1821 * Returns 0 for success.
1822 */
1823int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1825{
1826 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001827 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001828 unsigned int val, mux;
1829 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001830 int ret = 0;
1831
1832 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1833 return -EINVAL;
1834 mux = ucontrol->value.enumerated.item[0];
1835 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1836 mask = e->mask << e->shift_l;
1837 if (e->shift_l != e->shift_r) {
1838 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1839 return -EINVAL;
1840 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1841 mask |= e->mask << e->shift_r;
1842 }
1843
1844 mutex_lock(&widget->codec->mutex);
1845 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001846 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001847 if (widget->event) {
1848 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1849 ret = widget->event(widget,
1850 kcontrol, SND_SOC_DAPM_PRE_REG);
1851 if (ret < 0)
1852 goto out;
1853 }
1854 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1855 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1856 ret = widget->event(widget,
1857 kcontrol, SND_SOC_DAPM_POST_REG);
1858 } else
1859 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1860
1861out:
1862 mutex_unlock(&widget->codec->mutex);
1863 return ret;
1864}
1865EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1866
1867/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001868 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1869 *
1870 * @kcontrol: mixer control
1871 * @uinfo: control element information
1872 *
1873 * Callback to provide information about a pin switch control.
1874 */
1875int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1876 struct snd_ctl_elem_info *uinfo)
1877{
1878 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1879 uinfo->count = 1;
1880 uinfo->value.integer.min = 0;
1881 uinfo->value.integer.max = 1;
1882
1883 return 0;
1884}
1885EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1886
1887/**
1888 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1889 *
1890 * @kcontrol: mixer control
1891 * @ucontrol: Value
1892 */
1893int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1894 struct snd_ctl_elem_value *ucontrol)
1895{
1896 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1897 const char *pin = (const char *)kcontrol->private_value;
1898
1899 mutex_lock(&codec->mutex);
1900
1901 ucontrol->value.integer.value[0] =
1902 snd_soc_dapm_get_pin_status(codec, pin);
1903
1904 mutex_unlock(&codec->mutex);
1905
1906 return 0;
1907}
1908EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1909
1910/**
1911 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1912 *
1913 * @kcontrol: mixer control
1914 * @ucontrol: Value
1915 */
1916int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1917 struct snd_ctl_elem_value *ucontrol)
1918{
1919 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1920 const char *pin = (const char *)kcontrol->private_value;
1921
1922 mutex_lock(&codec->mutex);
1923
1924 if (ucontrol->value.integer.value[0])
1925 snd_soc_dapm_enable_pin(codec, pin);
1926 else
1927 snd_soc_dapm_disable_pin(codec, pin);
1928
1929 snd_soc_dapm_sync(codec);
1930
1931 mutex_unlock(&codec->mutex);
1932
1933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1936
1937/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938 * snd_soc_dapm_new_control - create new dapm control
1939 * @codec: audio codec
1940 * @widget: widget template
1941 *
1942 * Creates a new dapm control based upon the template.
1943 *
1944 * Returns 0 for success else error.
1945 */
1946int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1947 const struct snd_soc_dapm_widget *widget)
1948{
1949 struct snd_soc_dapm_widget *w;
1950
1951 if ((w = dapm_cnew_widget(widget)) == NULL)
1952 return -ENOMEM;
1953
1954 w->codec = codec;
1955 INIT_LIST_HEAD(&w->sources);
1956 INIT_LIST_HEAD(&w->sinks);
1957 INIT_LIST_HEAD(&w->list);
1958 list_add(&w->list, &codec->dapm_widgets);
1959
1960 /* machine layer set ups unconnected pins and insertions */
1961 w->connected = 1;
1962 return 0;
1963}
1964EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1965
1966/**
Mark Brown4ba13272008-05-13 14:51:19 +02001967 * snd_soc_dapm_new_controls - create new dapm controls
1968 * @codec: audio codec
1969 * @widget: widget array
1970 * @num: number of widgets
1971 *
1972 * Creates new DAPM controls based upon the templates.
1973 *
1974 * Returns 0 for success else error.
1975 */
1976int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1977 const struct snd_soc_dapm_widget *widget,
1978 int num)
1979{
1980 int i, ret;
1981
1982 for (i = 0; i < num; i++) {
1983 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001984 if (ret < 0) {
1985 printk(KERN_ERR
1986 "ASoC: Failed to create DAPM control %s: %d\n",
1987 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001988 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001989 }
Mark Brown4ba13272008-05-13 14:51:19 +02001990 widget++;
1991 }
1992 return 0;
1993}
1994EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1995
1996
1997/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1999 * @codec: audio codec
2000 * @stream: stream name
2001 * @event: stream event
2002 *
2003 * Sends a stream event to the dapm core. The core then makes any
2004 * necessary widget power changes.
2005 *
2006 * Returns 0 for success else error.
2007 */
2008int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2009 char *stream, int event)
2010{
2011 struct snd_soc_dapm_widget *w;
2012
Seth Forshee11da21a2007-02-02 17:14:19 +01002013 if (stream == NULL)
2014 return 0;
2015
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 mutex_lock(&codec->mutex);
2017 list_for_each_entry(w, &codec->dapm_widgets, list)
2018 {
2019 if (!w->sname)
2020 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002021 pr_debug("widget %s\n %s stream %s event %d\n",
2022 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 if (strstr(w->sname, stream)) {
2024 switch(event) {
2025 case SND_SOC_DAPM_STREAM_START:
2026 w->active = 1;
2027 break;
2028 case SND_SOC_DAPM_STREAM_STOP:
2029 w->active = 0;
2030 break;
2031 case SND_SOC_DAPM_STREAM_SUSPEND:
2032 if (w->active)
2033 w->suspend = 1;
2034 w->active = 0;
2035 break;
2036 case SND_SOC_DAPM_STREAM_RESUME:
2037 if (w->suspend) {
2038 w->active = 1;
2039 w->suspend = 0;
2040 }
2041 break;
2042 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2043 break;
2044 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2045 break;
2046 }
2047 }
2048 }
2049 mutex_unlock(&codec->mutex);
2050
2051 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08002052 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053 return 0;
2054}
2055EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2056
2057/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002058 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002059 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002060 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 *
Mark Brown74b8f952009-06-06 11:26:15 +01002062 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002063 * a valid audio route and active audio stream.
2064 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2065 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 */
Mark Brown16499232009-01-07 18:25:13 +00002067int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068{
Liam Girdwooda5302182008-07-07 13:35:17 +01002069 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070}
Liam Girdwooda5302182008-07-07 13:35:17 +01002071EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072
2073/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002074 * snd_soc_dapm_disable_pin - disable pin.
2075 * @codec: SoC codec
2076 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002077 *
Mark Brown74b8f952009-06-06 11:26:15 +01002078 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002079 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2080 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002081 */
Mark Brown16499232009-01-07 18:25:13 +00002082int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002083{
2084 return snd_soc_dapm_set_pin(codec, pin, 0);
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2087
2088/**
Mark Brown5817b522008-09-24 11:23:11 +01002089 * snd_soc_dapm_nc_pin - permanently disable pin.
2090 * @codec: SoC codec
2091 * @pin: pin name
2092 *
2093 * Marks the specified pin as being not connected, disabling it along
2094 * any parent or child widgets. At present this is identical to
2095 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2096 * additional things such as disabling controls which only affect
2097 * paths through the pin.
2098 *
2099 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2100 * do any widget power switching.
2101 */
Mark Brown16499232009-01-07 18:25:13 +00002102int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002103{
2104 return snd_soc_dapm_set_pin(codec, pin, 0);
2105}
2106EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2107
2108/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002109 * snd_soc_dapm_get_pin_status - get audio pin status
2110 * @codec: audio codec
2111 * @pin: audio signal pin endpoint (or start point)
2112 *
2113 * Get audio pin status - connected or disconnected.
2114 *
2115 * Returns 1 for connected otherwise 0.
2116 */
Mark Brown16499232009-01-07 18:25:13 +00002117int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002118{
2119 struct snd_soc_dapm_widget *w;
2120
2121 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002122 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002123 return w->connected;
2124 }
2125
2126 return 0;
2127}
Liam Girdwooda5302182008-07-07 13:35:17 +01002128EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002129
2130/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 * snd_soc_dapm_free - free dapm resources
2132 * @socdev: SoC device
2133 *
2134 * Free all dapm widgets and resources.
2135 */
2136void snd_soc_dapm_free(struct snd_soc_device *socdev)
2137{
Mark Brown6627a652009-01-23 22:55:23 +00002138 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139
2140 snd_soc_dapm_sys_remove(socdev->dev);
2141 dapm_free_widgets(codec);
2142}
2143EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2144
Mark Brown51737472009-06-22 13:16:51 +01002145/*
2146 * snd_soc_dapm_shutdown - callback for system shutdown
2147 */
2148void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2149{
2150 struct snd_soc_codec *codec = socdev->card->codec;
2151 struct snd_soc_dapm_widget *w;
2152 LIST_HEAD(down_list);
2153 int powerdown = 0;
2154
2155 list_for_each_entry(w, &codec->dapm_widgets, list) {
2156 if (w->power) {
2157 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002158 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002159 powerdown = 1;
2160 }
2161 }
2162
2163 /* If there were no widgets to power down we're already in
2164 * standby.
2165 */
2166 if (powerdown) {
2167 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2168 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2169 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2170 }
2171
2172 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2173}
2174
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002176MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2178MODULE_LICENSE("GPL");