blob: 6c3351095786f224448f62277d61aa583dfe000a [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
Richard Purdie2b97eab2006-10-06 18:32:18 +020047/* dapm power sequences - make this per codec in the future */
48static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010049 [snd_soc_dapm_pre] = 0,
50 [snd_soc_dapm_supply] = 1,
51 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010052 [snd_soc_dapm_aif_in] = 3,
53 [snd_soc_dapm_aif_out] = 3,
54 [snd_soc_dapm_mic] = 4,
55 [snd_soc_dapm_mux] = 5,
56 [snd_soc_dapm_value_mux] = 5,
57 [snd_soc_dapm_dac] = 6,
58 [snd_soc_dapm_mixer] = 7,
59 [snd_soc_dapm_mixer_named_ctl] = 7,
60 [snd_soc_dapm_pga] = 8,
61 [snd_soc_dapm_adc] = 9,
62 [snd_soc_dapm_hp] = 10,
63 [snd_soc_dapm_spk] = 10,
64 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020065};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000066
Richard Purdie2b97eab2006-10-06 18:32:18 +020067static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010068 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_adc] = 1,
70 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010071 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pga] = 4,
73 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010074 [snd_soc_dapm_mixer] = 5,
75 [snd_soc_dapm_dac] = 6,
76 [snd_soc_dapm_mic] = 7,
77 [snd_soc_dapm_micbias] = 8,
78 [snd_soc_dapm_mux] = 9,
79 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010080 [snd_soc_dapm_aif_in] = 10,
81 [snd_soc_dapm_aif_out] = 10,
82 [snd_soc_dapm_supply] = 11,
83 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020084};
85
Troy Kisky12ef1932008-10-13 17:42:14 -070086static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010087{
88 if (pop_time)
89 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
90}
91
Troy Kisky12ef1932008-10-13 17:42:14 -070092static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010093{
94 va_list args;
95
96 va_start(args, fmt);
97
98 if (pop_time) {
99 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700100 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100101 }
102
103 va_end(args);
104}
105
Richard Purdie2b97eab2006-10-06 18:32:18 +0200106/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100107static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200108 const struct snd_soc_dapm_widget *_widget)
109{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100110 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200111}
112
Mark Brown452c5ea2009-05-17 21:41:23 +0100113/**
114 * snd_soc_dapm_set_bias_level - set the bias level for the system
115 * @socdev: audio device
116 * @level: level to configure
117 *
118 * Configure the bias (power) levels for the SoC audio device.
119 *
120 * Returns 0 for success else error.
121 */
122static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
123 enum snd_soc_bias_level level)
124{
125 struct snd_soc_card *card = socdev->card;
126 struct snd_soc_codec *codec = socdev->card->codec;
127 int ret = 0;
128
Mark Brownf83fba82009-05-18 15:44:43 +0100129 switch (level) {
130 case SND_SOC_BIAS_ON:
131 dev_dbg(socdev->dev, "Setting full bias\n");
132 break;
133 case SND_SOC_BIAS_PREPARE:
134 dev_dbg(socdev->dev, "Setting bias prepare\n");
135 break;
136 case SND_SOC_BIAS_STANDBY:
137 dev_dbg(socdev->dev, "Setting standby bias\n");
138 break;
139 case SND_SOC_BIAS_OFF:
140 dev_dbg(socdev->dev, "Setting bias off\n");
141 break;
142 default:
143 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
144 return -EINVAL;
145 }
146
Mark Brown452c5ea2009-05-17 21:41:23 +0100147 if (card->set_bias_level)
148 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100149 if (ret == 0) {
150 if (codec->set_bias_level)
151 ret = codec->set_bias_level(codec, level);
152 else
153 codec->bias_level = level;
154 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100155
156 return ret;
157}
158
Richard Purdie2b97eab2006-10-06 18:32:18 +0200159/* set up initial codec paths */
160static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
161 struct snd_soc_dapm_path *p, int i)
162{
163 switch (w->id) {
164 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000165 case snd_soc_dapm_mixer:
166 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200167 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100168 struct soc_mixer_control *mc = (struct soc_mixer_control *)
169 w->kcontrols[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400170 unsigned int reg = mc->reg;
171 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100172 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400173 unsigned int mask = (1 << fls(max)) - 1;
174 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175
176 val = snd_soc_read(w->codec, reg);
177 val = (val >> shift) & mask;
178
179 if ((invert && !val) || (!invert && val))
180 p->connect = 1;
181 else
182 p->connect = 0;
183 }
184 break;
185 case snd_soc_dapm_mux: {
186 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
187 int val, item, bitmask;
188
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100189 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200190 ;
191 val = snd_soc_read(w->codec, e->reg);
192 item = (val >> e->shift_l) & (bitmask - 1);
193
194 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100195 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196 if (!(strcmp(p->name, e->texts[i])) && item == i)
197 p->connect = 1;
198 }
199 }
200 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200201 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200202 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200203 w->kcontrols[i].private_value;
204 int val, item;
205
206 val = snd_soc_read(w->codec, e->reg);
207 val = (val >> e->shift_l) & e->mask;
208 for (item = 0; item < e->max; item++) {
209 if (val == e->values[item])
210 break;
211 }
212
213 p->connect = 0;
214 for (i = 0; i < e->max; i++) {
215 if (!(strcmp(p->name, e->texts[i])) && item == i)
216 p->connect = 1;
217 }
218 }
219 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200220 /* does not effect routing - always connected */
221 case snd_soc_dapm_pga:
222 case snd_soc_dapm_output:
223 case snd_soc_dapm_adc:
224 case snd_soc_dapm_input:
225 case snd_soc_dapm_dac:
226 case snd_soc_dapm_micbias:
227 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100228 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100229 case snd_soc_dapm_aif_in:
230 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200231 p->connect = 1;
232 break;
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp:
235 case snd_soc_dapm_mic:
236 case snd_soc_dapm_spk:
237 case snd_soc_dapm_line:
238 case snd_soc_dapm_pre:
239 case snd_soc_dapm_post:
240 p->connect = 0;
241 break;
242 }
243}
244
Mark Brown74b8f952009-06-06 11:26:15 +0100245/* connect mux widget to its interconnecting audio paths */
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 Smirl815ecf82008-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 Smirl815ecf82008-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
Mark Brown010ff262009-08-17 17:39:22 +0100504 switch (widget->id) {
505 case snd_soc_dapm_adc:
506 case snd_soc_dapm_aif_out:
507 if (widget->active)
508 return 1;
509 default:
510 break;
511 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512
513 if (widget->connected) {
514 /* connected pin ? */
515 if (widget->id == snd_soc_dapm_output && !widget->ext)
516 return 1;
517
518 /* connected jack or spk ? */
519 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300520 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521 return 1;
522 }
523
524 list_for_each_entry(path, &widget->sinks, list_source) {
525 if (path->walked)
526 continue;
527
528 if (path->sink && path->connect) {
529 path->walked = 1;
530 con += is_connected_output_ep(path->sink);
531 }
532 }
533
534 return con;
535}
536
537/*
538 * Recursively check for a completed path to an active or physically connected
539 * input widget. Returns number of complete paths.
540 */
541static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
542{
543 struct snd_soc_dapm_path *path;
544 int con = 0;
545
Mark Brown246d0a12009-04-22 18:24:55 +0100546 if (widget->id == snd_soc_dapm_supply)
547 return 0;
548
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100550 switch (widget->id) {
551 case snd_soc_dapm_dac:
552 case snd_soc_dapm_aif_in:
553 if (widget->active)
554 return 1;
555 default:
556 break;
557 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200558
559 if (widget->connected) {
560 /* connected pin ? */
561 if (widget->id == snd_soc_dapm_input && !widget->ext)
562 return 1;
563
564 /* connected VMID/Bias for lower pops */
565 if (widget->id == snd_soc_dapm_vmid)
566 return 1;
567
568 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300569 if (widget->id == snd_soc_dapm_mic ||
570 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571 return 1;
572 }
573
574 list_for_each_entry(path, &widget->sources, list_sink) {
575 if (path->walked)
576 continue;
577
578 if (path->source && path->connect) {
579 path->walked = 1;
580 con += is_connected_input_ep(path->source);
581 }
582 }
583
584 return con;
585}
586
587/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300588 * Handler for generic register modifier widget.
589 */
590int dapm_reg_event(struct snd_soc_dapm_widget *w,
591 struct snd_kcontrol *kcontrol, int event)
592{
593 unsigned int val;
594
595 if (SND_SOC_DAPM_EVENT_ON(event))
596 val = w->on_val;
597 else
598 val = w->off_val;
599
600 snd_soc_update_bits(w->codec, -(w->reg + 1),
601 w->mask << w->shift, val << w->shift);
602
603 return 0;
604}
Mark Brown11589412008-07-29 11:42:23 +0100605EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300606
Mark Brown025756e2009-04-13 11:09:18 +0100607/* Standard power change method, used to apply power changes to most
608 * widgets.
609 */
610static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
611{
612 int ret;
613
614 /* call any power change event handlers */
615 if (w->event)
616 pr_debug("power %s event for %s flags %x\n",
617 w->power ? "on" : "off",
618 w->name, w->event_flags);
619
620 /* power up pre event */
621 if (w->power && w->event &&
622 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
623 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
624 if (ret < 0)
625 return ret;
626 }
627
628 /* power down pre event */
629 if (!w->power && w->event &&
630 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
631 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
632 if (ret < 0)
633 return ret;
634 }
635
636 /* Lower PGA volume to reduce pops */
637 if (w->id == snd_soc_dapm_pga && !w->power)
638 dapm_set_pga(w, w->power);
639
640 dapm_update_bits(w);
641
642 /* Raise PGA volume to reduce pops */
643 if (w->id == snd_soc_dapm_pga && w->power)
644 dapm_set_pga(w, w->power);
645
646 /* power up post event */
647 if (w->power && w->event &&
648 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
649 ret = w->event(w,
650 NULL, SND_SOC_DAPM_POST_PMU);
651 if (ret < 0)
652 return ret;
653 }
654
655 /* power down post event */
656 if (!w->power && w->event &&
657 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
658 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
659 if (ret < 0)
660 return ret;
661 }
662
663 return 0;
664}
665
Mark Browncd0f2d42009-04-20 16:56:59 +0100666/* Generic check to see if a widget should be powered.
667 */
668static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
669{
670 int in, out;
671
672 in = is_connected_input_ep(w);
673 dapm_clear_walk(w->codec);
674 out = is_connected_output_ep(w);
675 dapm_clear_walk(w->codec);
676 return out != 0 && in != 0;
677}
678
Mark Brown6ea31b92009-04-20 17:15:41 +0100679/* Check to see if an ADC has power */
680static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
681{
682 int in;
683
684 if (w->active) {
685 in = is_connected_input_ep(w);
686 dapm_clear_walk(w->codec);
687 return in != 0;
688 } else {
689 return dapm_generic_check_power(w);
690 }
691}
692
693/* Check to see if a DAC has power */
694static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
695{
696 int out;
697
698 if (w->active) {
699 out = is_connected_output_ep(w);
700 dapm_clear_walk(w->codec);
701 return out != 0;
702 } else {
703 return dapm_generic_check_power(w);
704 }
705}
706
Mark Brown246d0a12009-04-22 18:24:55 +0100707/* Check to see if a power supply is needed */
708static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
709{
710 struct snd_soc_dapm_path *path;
711 int power = 0;
712
713 /* Check if one of our outputs is connected */
714 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100715 if (path->connected &&
716 !path->connected(path->source, path->sink))
717 continue;
718
Mark Brown246d0a12009-04-22 18:24:55 +0100719 if (path->sink && path->sink->power_check &&
720 path->sink->power_check(path->sink)) {
721 power = 1;
722 break;
723 }
724 }
725
726 dapm_clear_walk(w->codec);
727
728 return power;
729}
730
Mark Brown38357ab2009-06-06 19:03:23 +0100731static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
732 struct snd_soc_dapm_widget *b,
733 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000734{
Mark Brownd207c682009-12-07 17:13:55 +0000735 if (a->codec != b->codec)
736 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100737 if (sort[a->id] != sort[b->id])
738 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100739 if (a->reg != b->reg)
740 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000741
Mark Brown38357ab2009-06-06 19:03:23 +0100742 return 0;
743}
Mark Brown42aa3412009-03-01 19:21:10 +0000744
Mark Brown38357ab2009-06-06 19:03:23 +0100745/* Insert a widget in order into a DAPM power sequence. */
746static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
747 struct list_head *list,
748 int sort[])
749{
750 struct snd_soc_dapm_widget *w;
751
752 list_for_each_entry(w, list, power_list)
753 if (dapm_seq_compare(new_widget, w, sort) < 0) {
754 list_add_tail(&new_widget->power_list, &w->power_list);
755 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000756 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100757
Mark Brown38357ab2009-06-06 19:03:23 +0100758 list_add_tail(&new_widget->power_list, list);
759}
Mark Brown42aa3412009-03-01 19:21:10 +0000760
Mark Brownb22ead22009-06-07 12:51:26 +0100761/* Apply the coalesced changes from a DAPM sequence */
762static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
763 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100764{
765 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100766 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100767 unsigned int value = 0;
768 unsigned int mask = 0;
769 unsigned int cur_mask;
770
771 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
772 power_list)->reg;
773
774 list_for_each_entry(w, pending, power_list) {
775 cur_mask = 1 << w->shift;
776 BUG_ON(reg != w->reg);
777
778 if (w->invert)
779 power = !w->power;
780 else
781 power = w->power;
782
783 mask |= cur_mask;
784 if (power)
785 value |= cur_mask;
786
787 pop_dbg(codec->pop_time,
788 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
789 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100790
791 /* power up pre event */
792 if (w->power && w->event &&
793 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
794 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
795 w->name);
796 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
797 if (ret < 0)
798 pr_err("%s: pre event failed: %d\n",
799 w->name, ret);
800 }
801
802 /* power down pre event */
803 if (!w->power && w->event &&
804 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
805 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
806 w->name);
807 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
808 if (ret < 0)
809 pr_err("%s: pre event failed: %d\n",
810 w->name, ret);
811 }
Mark Brown4f1c1922009-06-07 13:37:44 +0100812
813 /* Lower PGA volume to reduce pops */
814 if (w->id == snd_soc_dapm_pga && !w->power)
815 dapm_set_pga(w, w->power);
Mark Brownb22ead22009-06-07 12:51:26 +0100816 }
817
Mark Brown81628102009-06-07 13:21:24 +0100818 if (reg >= 0) {
819 pop_dbg(codec->pop_time,
820 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
821 value, mask, reg, codec->pop_time);
822 pop_wait(codec->pop_time);
823 snd_soc_update_bits(codec, reg, mask, value);
824 }
825
826 list_for_each_entry(w, pending, power_list) {
Mark Brown4f1c1922009-06-07 13:37:44 +0100827 /* Raise PGA volume to reduce pops */
828 if (w->id == snd_soc_dapm_pga && w->power)
829 dapm_set_pga(w, w->power);
830
Mark Brown81628102009-06-07 13:21:24 +0100831 /* power up post event */
832 if (w->power && w->event &&
833 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
834 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
835 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000836 ret = w->event(w,
837 NULL, SND_SOC_DAPM_POST_PMU);
838 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100839 pr_err("%s: post event failed: %d\n",
840 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000841 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100842
Mark Brown81628102009-06-07 13:21:24 +0100843 /* power down post event */
844 if (!w->power && w->event &&
845 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
846 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
847 w->name);
848 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
849 if (ret < 0)
850 pr_err("%s: post event failed: %d\n",
851 w->name, ret);
852 }
Mark Brown42aa3412009-03-01 19:21:10 +0000853 }
Mark Brown42aa3412009-03-01 19:21:10 +0000854}
855
Mark Brownb22ead22009-06-07 12:51:26 +0100856/* Apply a DAPM power sequence.
857 *
858 * We walk over a pre-sorted list of widgets to apply power to. In
859 * order to minimise the number of writes to the device required
860 * multiple widgets will be updated in a single write where possible.
861 * Currently anything that requires more than a single write is not
862 * handled.
863 */
864static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
865 int event, int sort[])
866{
867 struct snd_soc_dapm_widget *w, *n;
868 LIST_HEAD(pending);
869 int cur_sort = -1;
870 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100871 int ret;
872
Mark Brownb22ead22009-06-07 12:51:26 +0100873 list_for_each_entry_safe(w, n, list, power_list) {
874 ret = 0;
875
876 /* Do we need to apply any queued changes? */
877 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
878 if (!list_empty(&pending))
879 dapm_seq_run_coalesced(codec, &pending);
880
881 INIT_LIST_HEAD(&pending);
882 cur_sort = -1;
883 cur_reg = SND_SOC_NOPM;
884 }
885
Mark Brown163cac02009-06-07 10:12:52 +0100886 switch (w->id) {
887 case snd_soc_dapm_pre:
888 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100889 list_for_each_entry_safe_continue(w, n, list,
890 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100891
Mark Brownb22ead22009-06-07 12:51:26 +0100892 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100893 ret = w->event(w,
894 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100895 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100896 ret = w->event(w,
897 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100898 break;
899
900 case snd_soc_dapm_post:
901 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100902 list_for_each_entry_safe_continue(w, n, list,
903 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100904
Mark Brownb22ead22009-06-07 12:51:26 +0100905 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100906 ret = w->event(w,
907 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100908 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100909 ret = w->event(w,
910 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100911 break;
912
913 case snd_soc_dapm_input:
914 case snd_soc_dapm_output:
915 case snd_soc_dapm_hp:
916 case snd_soc_dapm_mic:
917 case snd_soc_dapm_line:
918 case snd_soc_dapm_spk:
919 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100920 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100921 break;
922
923 default:
Mark Brown81628102009-06-07 13:21:24 +0100924 /* Queue it up for application */
925 cur_sort = sort[w->id];
926 cur_reg = w->reg;
927 list_move(&w->power_list, &pending);
928 break;
Mark Brown163cac02009-06-07 10:12:52 +0100929 }
Mark Brownb22ead22009-06-07 12:51:26 +0100930
931 if (ret < 0)
932 pr_err("Failed to apply widget power: %d\n",
933 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100934 }
Mark Brownb22ead22009-06-07 12:51:26 +0100935
936 if (!list_empty(&pending))
937 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100938}
939
Mark Brown42aa3412009-03-01 19:21:10 +0000940/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 * Scan each dapm widget for complete audio path.
942 * A complete path is a route that has valid endpoints i.e.:-
943 *
944 * o DAC to output pin.
945 * o Input Pin to ADC.
946 * o Input pin to Output pin (bypass, sidetone)
947 * o DAC to ADC (loopback).
948 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100949static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950{
Mark Brown452c5ea2009-05-17 21:41:23 +0100951 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200952 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100953 LIST_HEAD(up_list);
954 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100955 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100956 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100957 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958
Mark Brown6d3ddc82009-05-16 17:47:29 +0100959 /* Check which widgets we need to power and store them in
960 * lists indicating if they should be powered up or down.
961 */
962 list_for_each_entry(w, &codec->dapm_widgets, list) {
963 switch (w->id) {
964 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100965 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100966 break;
967 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100968 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100969 break;
970
971 default:
972 if (!w->power_check)
973 continue;
974
Mark Brown50b6bce2009-11-23 13:11:53 +0000975 /* If we're suspending then pull down all the
976 * power. */
977 switch (event) {
978 case SND_SOC_DAPM_STREAM_SUSPEND:
979 power = 0;
980 break;
981
982 default:
983 power = w->power_check(w);
984 if (power)
985 sys_power = 1;
986 break;
987 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100988
Mark Brown6d3ddc82009-05-16 17:47:29 +0100989 if (w->power == power)
990 continue;
991
992 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100993 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100994 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100995 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100996
997 w->power = power;
998 break;
999 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001000 }
1001
Mark Brownb14b76a2009-08-17 11:55:38 +01001002 /* If there are no DAPM widgets then try to figure out power from the
1003 * event type.
1004 */
1005 if (list_empty(&codec->dapm_widgets)) {
1006 switch (event) {
1007 case SND_SOC_DAPM_STREAM_START:
1008 case SND_SOC_DAPM_STREAM_RESUME:
1009 sys_power = 1;
1010 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001011 case SND_SOC_DAPM_STREAM_SUSPEND:
1012 sys_power = 0;
1013 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001014 case SND_SOC_DAPM_STREAM_NOP:
Mark Browna96ca332010-01-19 22:49:43 +00001015 switch (codec->bias_level) {
1016 case SND_SOC_BIAS_STANDBY:
1017 case SND_SOC_BIAS_OFF:
1018 sys_power = 0;
1019 break;
1020 default:
1021 sys_power = 1;
1022 break;
1023 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001024 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001025 default:
1026 break;
1027 }
1028 }
1029
Mark Browna96ca332010-01-19 22:49:43 +00001030 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
1031 ret = snd_soc_dapm_set_bias_level(socdev,
1032 SND_SOC_BIAS_STANDBY);
1033 if (ret != 0)
1034 pr_err("Failed to turn on bias: %d\n", ret);
1035 }
1036
Mark Brown452c5ea2009-05-17 21:41:23 +01001037 /* If we're changing to all on or all off then prepare */
1038 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
1039 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
1040 ret = snd_soc_dapm_set_bias_level(socdev,
1041 SND_SOC_BIAS_PREPARE);
1042 if (ret != 0)
1043 pr_err("Failed to prepare bias: %d\n", ret);
1044 }
1045
Mark Brown6d3ddc82009-05-16 17:47:29 +01001046 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001047 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001048
1049 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001050 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001051
Mark Brown452c5ea2009-05-17 21:41:23 +01001052 /* If we just powered the last thing off drop to standby bias */
1053 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1054 ret = snd_soc_dapm_set_bias_level(socdev,
1055 SND_SOC_BIAS_STANDBY);
1056 if (ret != 0)
1057 pr_err("Failed to apply standby bias: %d\n", ret);
1058 }
1059
Mark Browna96ca332010-01-19 22:49:43 +00001060 /* If we're in standby and can support bias off then do that */
1061 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
1062 codec->idle_bias_off) {
1063 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1064 if (ret != 0)
1065 pr_err("Failed to turn off bias: %d\n", ret);
1066 }
1067
Mark Brown452c5ea2009-05-17 21:41:23 +01001068 /* If we just powered up then move to active bias */
1069 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1070 ret = snd_soc_dapm_set_bias_level(socdev,
1071 SND_SOC_BIAS_ON);
1072 if (ret != 0)
1073 pr_err("Failed to apply active bias: %d\n", ret);
1074 }
1075
Mark Browncb507e72009-07-08 18:54:57 +01001076 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1077 codec->pop_time);
1078
Mark Brown42aa3412009-03-01 19:21:10 +00001079 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001080}
1081
Mark Brown79fb9382009-08-21 16:38:13 +01001082#ifdef CONFIG_DEBUG_FS
1083static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1084{
1085 file->private_data = inode->i_private;
1086 return 0;
1087}
1088
1089static ssize_t dapm_widget_power_read_file(struct file *file,
1090 char __user *user_buf,
1091 size_t count, loff_t *ppos)
1092{
1093 struct snd_soc_dapm_widget *w = file->private_data;
1094 char *buf;
1095 int in, out;
1096 ssize_t ret;
1097 struct snd_soc_dapm_path *p = NULL;
1098
1099 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1100 if (!buf)
1101 return -ENOMEM;
1102
1103 in = is_connected_input_ep(w);
1104 dapm_clear_walk(w->codec);
1105 out = is_connected_output_ep(w);
1106 dapm_clear_walk(w->codec);
1107
Mark Brownd033c362009-12-04 15:25:56 +00001108 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001109 w->name, w->power ? "On" : "Off", in, out);
1110
Mark Brownd033c362009-12-04 15:25:56 +00001111 if (w->reg >= 0)
1112 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1113 " - R%d(0x%x) bit %d",
1114 w->reg, w->reg, w->shift);
1115
1116 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1117
Mark Brown3eef08b2009-09-14 16:49:00 +01001118 if (w->sname)
1119 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1120 w->sname,
1121 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001122
1123 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001124 if (p->connected && !p->connected(w, p->sink))
1125 continue;
1126
Mark Brown79fb9382009-08-21 16:38:13 +01001127 if (p->connect)
1128 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1129 " in %s %s\n",
1130 p->name ? p->name : "static",
1131 p->source->name);
1132 }
1133 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001134 if (p->connected && !p->connected(w, p->sink))
1135 continue;
1136
Mark Brown79fb9382009-08-21 16:38:13 +01001137 if (p->connect)
1138 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1139 " out %s %s\n",
1140 p->name ? p->name : "static",
1141 p->sink->name);
1142 }
1143
1144 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1145
1146 kfree(buf);
1147 return ret;
1148}
1149
1150static const struct file_operations dapm_widget_power_fops = {
1151 .open = dapm_widget_power_open_file,
1152 .read = dapm_widget_power_read_file,
1153};
1154
1155void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1156{
1157 struct snd_soc_dapm_widget *w;
1158 struct dentry *d;
1159
1160 if (!codec->debugfs_dapm)
1161 return;
1162
1163 list_for_each_entry(w, &codec->dapm_widgets, list) {
1164 if (!w->name)
1165 continue;
1166
1167 d = debugfs_create_file(w->name, 0444,
1168 codec->debugfs_dapm, w,
1169 &dapm_widget_power_fops);
1170 if (!d)
1171 printk(KERN_WARNING
1172 "ASoC: Failed to create %s debugfs file\n",
1173 w->name);
1174 }
1175}
1176#else
1177void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1178{
1179}
1180#endif
1181
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001183static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001184 struct snd_kcontrol *kcontrol, int change,
1185 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186{
1187 struct snd_soc_dapm_path *path;
1188 int found = 0;
1189
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001190 if (widget->id != snd_soc_dapm_mux &&
1191 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001192 return -ENODEV;
1193
Mark Brown3a655772009-10-05 17:23:30 +01001194 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 return 0;
1196
1197 /* find dapm widget path assoc with kcontrol */
1198 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1199 if (path->kcontrol != kcontrol)
1200 continue;
1201
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001202 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001203 continue;
1204
1205 found = 1;
1206 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001207 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001208 path->connect = 1; /* new connection */
1209 else
1210 path->connect = 0; /* old connection must be powered down */
1211 }
1212
Mark Brownb91b8fa2010-01-20 18:18:35 +00001213 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1215
1216 return 0;
1217}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218
Milan plzik1b075e32008-01-10 14:39:46 +01001219/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001220static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001221 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001222{
1223 struct snd_soc_dapm_path *path;
1224 int found = 0;
1225
Milan plzik1b075e32008-01-10 14:39:46 +01001226 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001227 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001228 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229 return -ENODEV;
1230
Richard Purdie2b97eab2006-10-06 18:32:18 +02001231 /* find dapm widget path assoc with kcontrol */
1232 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1233 if (path->kcontrol != kcontrol)
1234 continue;
1235
1236 /* found, now check type */
1237 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001238 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001239 break;
1240 }
1241
Mark Brownb91b8fa2010-01-20 18:18:35 +00001242 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001243 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1244
1245 return 0;
1246}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001247
1248/* show dapm widget status in sys fs */
1249static ssize_t dapm_widget_show(struct device *dev,
1250 struct device_attribute *attr, char *buf)
1251{
1252 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001253 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001254 struct snd_soc_dapm_widget *w;
1255 int count = 0;
1256 char *state = "not set";
1257
1258 list_for_each_entry(w, &codec->dapm_widgets, list) {
1259
1260 /* only display widgets that burnm power */
1261 switch (w->id) {
1262 case snd_soc_dapm_hp:
1263 case snd_soc_dapm_mic:
1264 case snd_soc_dapm_spk:
1265 case snd_soc_dapm_line:
1266 case snd_soc_dapm_micbias:
1267 case snd_soc_dapm_dac:
1268 case snd_soc_dapm_adc:
1269 case snd_soc_dapm_pga:
1270 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001271 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001272 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 if (w->name)
1274 count += sprintf(buf + count, "%s: %s\n",
1275 w->name, w->power ? "On":"Off");
1276 break;
1277 default:
1278 break;
1279 }
1280 }
1281
Mark Brown0be98982008-05-19 12:31:28 +02001282 switch (codec->bias_level) {
1283 case SND_SOC_BIAS_ON:
1284 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001285 break;
Mark Brown0be98982008-05-19 12:31:28 +02001286 case SND_SOC_BIAS_PREPARE:
1287 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001288 break;
Mark Brown0be98982008-05-19 12:31:28 +02001289 case SND_SOC_BIAS_STANDBY:
1290 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001291 break;
Mark Brown0be98982008-05-19 12:31:28 +02001292 case SND_SOC_BIAS_OFF:
1293 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294 break;
1295 }
1296 count += sprintf(buf + count, "PM State: %s\n", state);
1297
1298 return count;
1299}
1300
1301static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1302
1303int snd_soc_dapm_sys_add(struct device *dev)
1304{
Troy Kisky12ef1932008-10-13 17:42:14 -07001305 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001306}
1307
1308static void snd_soc_dapm_sys_remove(struct device *dev)
1309{
Mark Brownaef90842009-05-16 17:53:16 +01001310 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001311}
1312
1313/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001314static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001315{
1316 struct snd_soc_dapm_widget *w, *next_w;
1317 struct snd_soc_dapm_path *p, *next_p;
1318
1319 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1320 list_del(&w->list);
1321 kfree(w);
1322 }
1323
1324 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1325 list_del(&p->list);
1326 kfree(p->long_name);
1327 kfree(p);
1328 }
1329}
1330
Liam Girdwooda5302182008-07-07 13:35:17 +01001331static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001332 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001333{
1334 struct snd_soc_dapm_widget *w;
1335
1336 list_for_each_entry(w, &codec->dapm_widgets, list) {
1337 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001338 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001339 w->connected = status;
1340 return 0;
1341 }
1342 }
1343
Mark Brownc1286b82008-07-07 19:26:03 +01001344 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001345 return -EINVAL;
1346}
1347
Richard Purdie2b97eab2006-10-06 18:32:18 +02001348/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001349 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001350 * @codec: audio codec
1351 *
1352 * Walks all dapm audio paths and powers widgets according to their
1353 * stream or path usage.
1354 *
1355 * Returns 0 for success.
1356 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001357int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001358{
Mark Brownb91b8fa2010-01-20 18:18:35 +00001359 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001360}
Liam Girdwooda5302182008-07-07 13:35:17 +01001361EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001362
Mark Brown105f1c22008-05-13 14:52:19 +02001363static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001364 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365{
1366 struct snd_soc_dapm_path *path;
1367 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001368 const char *sink = route->sink;
1369 const char *control = route->control;
1370 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001371 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;
Mark Brown215edda2009-09-08 18:59:05 +01001394 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001395 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:
Mark Brown010ff262009-08-17 17:39:22 +01001436 case snd_soc_dapm_aif_in:
1437 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001438 list_add(&path->list, &codec->dapm_paths);
1439 list_add(&path->list_sink, &wsink->sources);
1440 list_add(&path->list_source, &wsource->sinks);
1441 path->connect = 1;
1442 return 0;
1443 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001444 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001445 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1446 &wsink->kcontrols[0]);
1447 if (ret != 0)
1448 goto err;
1449 break;
1450 case snd_soc_dapm_switch:
1451 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001452 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001453 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1454 if (ret != 0)
1455 goto err;
1456 break;
1457 case snd_soc_dapm_hp:
1458 case snd_soc_dapm_mic:
1459 case snd_soc_dapm_line:
1460 case snd_soc_dapm_spk:
1461 list_add(&path->list, &codec->dapm_paths);
1462 list_add(&path->list_sink, &wsink->sources);
1463 list_add(&path->list_source, &wsource->sinks);
1464 path->connect = 0;
1465 return 0;
1466 }
1467 return 0;
1468
1469err:
1470 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1471 control, sink);
1472 kfree(path);
1473 return ret;
1474}
Mark Brown105f1c22008-05-13 14:52:19 +02001475
1476/**
Mark Brown105f1c22008-05-13 14:52:19 +02001477 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1478 * @codec: codec
1479 * @route: audio routes
1480 * @num: number of routes
1481 *
1482 * Connects 2 dapm widgets together via a named audio path. The sink is
1483 * the widget receiving the audio signal, whilst the source is the sender
1484 * of the audio signal.
1485 *
1486 * Returns 0 for success else error. On error all resources can be freed
1487 * with a call to snd_soc_card_free().
1488 */
1489int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1490 const struct snd_soc_dapm_route *route, int num)
1491{
1492 int i, ret;
1493
1494 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001495 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001496 if (ret < 0) {
1497 printk(KERN_ERR "Failed to add route %s->%s\n",
1498 route->source,
1499 route->sink);
1500 return ret;
1501 }
1502 route++;
1503 }
1504
1505 return 0;
1506}
1507EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1508
1509/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001510 * snd_soc_dapm_new_widgets - add new dapm widgets
1511 * @codec: audio codec
1512 *
1513 * Checks the codec for any new dapm widgets and creates them if found.
1514 *
1515 * Returns 0 for success.
1516 */
1517int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1518{
1519 struct snd_soc_dapm_widget *w;
1520
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521 list_for_each_entry(w, &codec->dapm_widgets, list)
1522 {
1523 if (w->new)
1524 continue;
1525
1526 switch(w->id) {
1527 case snd_soc_dapm_switch:
1528 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001529 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001530 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531 dapm_new_mixer(codec, w);
1532 break;
1533 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001534 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001535 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536 dapm_new_mux(codec, w);
1537 break;
1538 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001539 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001540 w->power_check = dapm_adc_check_power;
1541 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001543 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001544 w->power_check = dapm_dac_check_power;
1545 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001547 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001548 dapm_new_pga(codec, w);
1549 break;
1550 case snd_soc_dapm_input:
1551 case snd_soc_dapm_output:
1552 case snd_soc_dapm_micbias:
1553 case snd_soc_dapm_spk:
1554 case snd_soc_dapm_hp:
1555 case snd_soc_dapm_mic:
1556 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001557 w->power_check = dapm_generic_check_power;
1558 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001559 case snd_soc_dapm_supply:
1560 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001561 case snd_soc_dapm_vmid:
1562 case snd_soc_dapm_pre:
1563 case snd_soc_dapm_post:
1564 break;
1565 }
1566 w->new = 1;
1567 }
1568
1569 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001570 return 0;
1571}
1572EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1573
1574/**
1575 * snd_soc_dapm_get_volsw - dapm mixer get callback
1576 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001577 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578 *
1579 * Callback to get the value of a dapm mixer control.
1580 *
1581 * Returns 0 for success.
1582 */
1583int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_value *ucontrol)
1585{
1586 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001587 struct soc_mixer_control *mc =
1588 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001589 unsigned int reg = mc->reg;
1590 unsigned int shift = mc->shift;
1591 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001592 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001593 unsigned int invert = mc->invert;
1594 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001595
1596 /* return the saved value if we are powered down */
1597 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1598 ucontrol->value.integer.value[0] = widget->saved_value;
1599 return 0;
1600 }
1601
1602 ucontrol->value.integer.value[0] =
1603 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1604 if (shift != rshift)
1605 ucontrol->value.integer.value[1] =
1606 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1607 if (invert) {
1608 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001609 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610 if (shift != rshift)
1611 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001612 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 }
1614
1615 return 0;
1616}
1617EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1618
1619/**
1620 * snd_soc_dapm_put_volsw - dapm mixer set callback
1621 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001622 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001623 *
1624 * Callback to set the value of a dapm mixer control.
1625 *
1626 * Returns 0 for success.
1627 */
1628int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_value *ucontrol)
1630{
1631 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001632 struct soc_mixer_control *mc =
1633 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001634 unsigned int reg = mc->reg;
1635 unsigned int shift = mc->shift;
1636 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001637 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001638 unsigned int mask = (1 << fls(max)) - 1;
1639 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001640 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001641 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001642 int ret;
1643
1644 val = (ucontrol->value.integer.value[0] & mask);
1645
1646 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001647 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001648 val_mask = mask << shift;
1649 val = val << shift;
1650 if (shift != rshift) {
1651 val2 = (ucontrol->value.integer.value[1] & mask);
1652 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001653 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001654 val_mask |= mask << rshift;
1655 val |= val2 << rshift;
1656 }
1657
1658 mutex_lock(&widget->codec->mutex);
1659 widget->value = val;
1660
1661 /* save volume value if the widget is powered down */
1662 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1663 widget->saved_value = val;
1664 mutex_unlock(&widget->codec->mutex);
1665 return 1;
1666 }
1667
Mark Brown283375c2009-12-07 18:09:03 +00001668 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1669 if (val)
1670 /* new connection */
1671 connect = invert ? 0:1;
1672 else
1673 /* old connection must be powered down */
1674 connect = invert ? 1:0;
1675
1676 dapm_mixer_update_power(widget, kcontrol, connect);
1677 }
1678
Richard Purdie2b97eab2006-10-06 18:32:18 +02001679 if (widget->event) {
1680 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001681 ret = widget->event(widget, kcontrol,
1682 SND_SOC_DAPM_PRE_REG);
1683 if (ret < 0) {
1684 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001685 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001686 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001687 }
1688 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1689 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001690 ret = widget->event(widget, kcontrol,
1691 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692 } else
1693 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1694
1695out:
1696 mutex_unlock(&widget->codec->mutex);
1697 return ret;
1698}
1699EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1700
1701/**
1702 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1703 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001704 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705 *
1706 * Callback to get the value of a dapm enumerated double mixer control.
1707 *
1708 * Returns 0 for success.
1709 */
1710int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1711 struct snd_ctl_elem_value *ucontrol)
1712{
1713 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1714 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001715 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001716
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001717 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 ;
1719 val = snd_soc_read(widget->codec, e->reg);
1720 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1721 if (e->shift_l != e->shift_r)
1722 ucontrol->value.enumerated.item[1] =
1723 (val >> e->shift_r) & (bitmask - 1);
1724
1725 return 0;
1726}
1727EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1728
1729/**
1730 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1731 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001732 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001733 *
1734 * Callback to set the value of a dapm enumerated double mixer control.
1735 *
1736 * Returns 0 for success.
1737 */
1738int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol)
1740{
1741 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1742 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001743 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001744 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001745 int ret = 0;
1746
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001747 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001748 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001749 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 return -EINVAL;
1751 mux = ucontrol->value.enumerated.item[0];
1752 val = mux << e->shift_l;
1753 mask = (bitmask - 1) << e->shift_l;
1754 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001755 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756 return -EINVAL;
1757 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1758 mask |= (bitmask - 1) << e->shift_r;
1759 }
1760
1761 mutex_lock(&widget->codec->mutex);
1762 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001763 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1764 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001765
1766 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1767 ret = widget->event(widget,
1768 kcontrol, SND_SOC_DAPM_PRE_REG);
1769 if (ret < 0)
1770 goto out;
1771 }
1772
1773 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1774
1775 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1776 ret = widget->event(widget,
1777 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001778
1779out:
1780 mutex_unlock(&widget->codec->mutex);
1781 return ret;
1782}
1783EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1784
1785/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001786 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1787 * @kcontrol: mixer control
1788 * @ucontrol: control element information
1789 *
1790 * Returns 0 for success.
1791 */
1792int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1793 struct snd_ctl_elem_value *ucontrol)
1794{
1795 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1796
1797 ucontrol->value.enumerated.item[0] = widget->value;
1798
1799 return 0;
1800}
1801EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1802
1803/**
1804 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1805 * @kcontrol: mixer control
1806 * @ucontrol: control element information
1807 *
1808 * Returns 0 for success.
1809 */
1810int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_value *ucontrol)
1812{
1813 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1814 struct soc_enum *e =
1815 (struct soc_enum *)kcontrol->private_value;
1816 int change;
1817 int ret = 0;
1818
1819 if (ucontrol->value.enumerated.item[0] >= e->max)
1820 return -EINVAL;
1821
1822 mutex_lock(&widget->codec->mutex);
1823
1824 change = widget->value != ucontrol->value.enumerated.item[0];
1825 widget->value = ucontrol->value.enumerated.item[0];
1826 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1827
1828 mutex_unlock(&widget->codec->mutex);
1829 return ret;
1830}
1831EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1832
1833/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001834 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1835 * callback
1836 * @kcontrol: mixer control
1837 * @ucontrol: control element information
1838 *
1839 * Callback to get the value of a dapm semi enumerated double mixer control.
1840 *
1841 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1842 * used for handling bitfield coded enumeration for example.
1843 *
1844 * Returns 0 for success.
1845 */
1846int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1848{
1849 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001850 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001851 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001852
1853 reg_val = snd_soc_read(widget->codec, e->reg);
1854 val = (reg_val >> e->shift_l) & e->mask;
1855 for (mux = 0; mux < e->max; mux++) {
1856 if (val == e->values[mux])
1857 break;
1858 }
1859 ucontrol->value.enumerated.item[0] = mux;
1860 if (e->shift_l != e->shift_r) {
1861 val = (reg_val >> e->shift_r) & e->mask;
1862 for (mux = 0; mux < e->max; mux++) {
1863 if (val == e->values[mux])
1864 break;
1865 }
1866 ucontrol->value.enumerated.item[1] = mux;
1867 }
1868
1869 return 0;
1870}
1871EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1872
1873/**
1874 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1875 * callback
1876 * @kcontrol: mixer control
1877 * @ucontrol: control element information
1878 *
1879 * Callback to set the value of a dapm semi enumerated double mixer control.
1880 *
1881 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1882 * used for handling bitfield coded enumeration for example.
1883 *
1884 * Returns 0 for success.
1885 */
1886int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1887 struct snd_ctl_elem_value *ucontrol)
1888{
1889 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001890 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001891 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001892 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001893 int ret = 0;
1894
1895 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1896 return -EINVAL;
1897 mux = ucontrol->value.enumerated.item[0];
1898 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1899 mask = e->mask << e->shift_l;
1900 if (e->shift_l != e->shift_r) {
1901 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1902 return -EINVAL;
1903 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1904 mask |= e->mask << e->shift_r;
1905 }
1906
1907 mutex_lock(&widget->codec->mutex);
1908 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001909 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1910 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001911
1912 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1913 ret = widget->event(widget,
1914 kcontrol, SND_SOC_DAPM_PRE_REG);
1915 if (ret < 0)
1916 goto out;
1917 }
1918
1919 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1920
1921 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1922 ret = widget->event(widget,
1923 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001924
1925out:
1926 mutex_unlock(&widget->codec->mutex);
1927 return ret;
1928}
1929EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1930
1931/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001932 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1933 *
1934 * @kcontrol: mixer control
1935 * @uinfo: control element information
1936 *
1937 * Callback to provide information about a pin switch control.
1938 */
1939int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_info *uinfo)
1941{
1942 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1943 uinfo->count = 1;
1944 uinfo->value.integer.min = 0;
1945 uinfo->value.integer.max = 1;
1946
1947 return 0;
1948}
1949EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1950
1951/**
1952 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1953 *
1954 * @kcontrol: mixer control
1955 * @ucontrol: Value
1956 */
1957int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1958 struct snd_ctl_elem_value *ucontrol)
1959{
1960 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1961 const char *pin = (const char *)kcontrol->private_value;
1962
1963 mutex_lock(&codec->mutex);
1964
1965 ucontrol->value.integer.value[0] =
1966 snd_soc_dapm_get_pin_status(codec, pin);
1967
1968 mutex_unlock(&codec->mutex);
1969
1970 return 0;
1971}
1972EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1973
1974/**
1975 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1976 *
1977 * @kcontrol: mixer control
1978 * @ucontrol: Value
1979 */
1980int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
1982{
1983 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1984 const char *pin = (const char *)kcontrol->private_value;
1985
1986 mutex_lock(&codec->mutex);
1987
1988 if (ucontrol->value.integer.value[0])
1989 snd_soc_dapm_enable_pin(codec, pin);
1990 else
1991 snd_soc_dapm_disable_pin(codec, pin);
1992
1993 snd_soc_dapm_sync(codec);
1994
1995 mutex_unlock(&codec->mutex);
1996
1997 return 0;
1998}
1999EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2000
2001/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002 * snd_soc_dapm_new_control - create new dapm control
2003 * @codec: audio codec
2004 * @widget: widget template
2005 *
2006 * Creates a new dapm control based upon the template.
2007 *
2008 * Returns 0 for success else error.
2009 */
2010int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
2011 const struct snd_soc_dapm_widget *widget)
2012{
2013 struct snd_soc_dapm_widget *w;
2014
2015 if ((w = dapm_cnew_widget(widget)) == NULL)
2016 return -ENOMEM;
2017
2018 w->codec = codec;
2019 INIT_LIST_HEAD(&w->sources);
2020 INIT_LIST_HEAD(&w->sinks);
2021 INIT_LIST_HEAD(&w->list);
2022 list_add(&w->list, &codec->dapm_widgets);
2023
2024 /* machine layer set ups unconnected pins and insertions */
2025 w->connected = 1;
2026 return 0;
2027}
2028EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2029
2030/**
Mark Brown4ba13272008-05-13 14:51:19 +02002031 * snd_soc_dapm_new_controls - create new dapm controls
2032 * @codec: audio codec
2033 * @widget: widget array
2034 * @num: number of widgets
2035 *
2036 * Creates new DAPM controls based upon the templates.
2037 *
2038 * Returns 0 for success else error.
2039 */
2040int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
2041 const struct snd_soc_dapm_widget *widget,
2042 int num)
2043{
2044 int i, ret;
2045
2046 for (i = 0; i < num; i++) {
2047 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002048 if (ret < 0) {
2049 printk(KERN_ERR
2050 "ASoC: Failed to create DAPM control %s: %d\n",
2051 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002052 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002053 }
Mark Brown4ba13272008-05-13 14:51:19 +02002054 widget++;
2055 }
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2059
2060
2061/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2063 * @codec: audio codec
2064 * @stream: stream name
2065 * @event: stream event
2066 *
2067 * Sends a stream event to the dapm core. The core then makes any
2068 * necessary widget power changes.
2069 *
2070 * Returns 0 for success else error.
2071 */
2072int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2073 char *stream, int event)
2074{
2075 struct snd_soc_dapm_widget *w;
2076
Seth Forshee11da21a2007-02-02 17:14:19 +01002077 if (stream == NULL)
2078 return 0;
2079
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 mutex_lock(&codec->mutex);
2081 list_for_each_entry(w, &codec->dapm_widgets, list)
2082 {
2083 if (!w->sname)
2084 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002085 pr_debug("widget %s\n %s stream %s event %d\n",
2086 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 if (strstr(w->sname, stream)) {
2088 switch(event) {
2089 case SND_SOC_DAPM_STREAM_START:
2090 w->active = 1;
2091 break;
2092 case SND_SOC_DAPM_STREAM_STOP:
2093 w->active = 0;
2094 break;
2095 case SND_SOC_DAPM_STREAM_SUSPEND:
2096 if (w->active)
2097 w->suspend = 1;
2098 w->active = 0;
2099 break;
2100 case SND_SOC_DAPM_STREAM_RESUME:
2101 if (w->suspend) {
2102 w->active = 1;
2103 w->suspend = 0;
2104 }
2105 break;
2106 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2107 break;
2108 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2109 break;
2110 }
2111 }
2112 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113
2114 dapm_power_widgets(codec, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002115 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 return 0;
2117}
2118EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2119
2120/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002121 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002122 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002123 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 *
Mark Brown74b8f952009-06-06 11:26:15 +01002125 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002126 * a valid audio route and active audio stream.
2127 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2128 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 */
Mark Brown16499232009-01-07 18:25:13 +00002130int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131{
Liam Girdwooda5302182008-07-07 13:35:17 +01002132 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133}
Liam Girdwooda5302182008-07-07 13:35:17 +01002134EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135
2136/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002137 * snd_soc_dapm_disable_pin - disable pin.
2138 * @codec: SoC codec
2139 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002140 *
Mark Brown74b8f952009-06-06 11:26:15 +01002141 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002142 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2143 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002144 */
Mark Brown16499232009-01-07 18:25:13 +00002145int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002146{
2147 return snd_soc_dapm_set_pin(codec, pin, 0);
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2150
2151/**
Mark Brown5817b522008-09-24 11:23:11 +01002152 * snd_soc_dapm_nc_pin - permanently disable pin.
2153 * @codec: SoC codec
2154 * @pin: pin name
2155 *
2156 * Marks the specified pin as being not connected, disabling it along
2157 * any parent or child widgets. At present this is identical to
2158 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2159 * additional things such as disabling controls which only affect
2160 * paths through the pin.
2161 *
2162 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2163 * do any widget power switching.
2164 */
Mark Brown16499232009-01-07 18:25:13 +00002165int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002166{
2167 return snd_soc_dapm_set_pin(codec, pin, 0);
2168}
2169EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2170
2171/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002172 * snd_soc_dapm_get_pin_status - get audio pin status
2173 * @codec: audio codec
2174 * @pin: audio signal pin endpoint (or start point)
2175 *
2176 * Get audio pin status - connected or disconnected.
2177 *
2178 * Returns 1 for connected otherwise 0.
2179 */
Mark Brown16499232009-01-07 18:25:13 +00002180int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002181{
2182 struct snd_soc_dapm_widget *w;
2183
2184 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002185 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002186 return w->connected;
2187 }
2188
2189 return 0;
2190}
Liam Girdwooda5302182008-07-07 13:35:17 +01002191EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002192
2193/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194 * snd_soc_dapm_free - free dapm resources
2195 * @socdev: SoC device
2196 *
2197 * Free all dapm widgets and resources.
2198 */
2199void snd_soc_dapm_free(struct snd_soc_device *socdev)
2200{
Mark Brown6627a652009-01-23 22:55:23 +00002201 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002202
2203 snd_soc_dapm_sys_remove(socdev->dev);
2204 dapm_free_widgets(codec);
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2207
Mark Brown51737472009-06-22 13:16:51 +01002208/*
2209 * snd_soc_dapm_shutdown - callback for system shutdown
2210 */
2211void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2212{
2213 struct snd_soc_codec *codec = socdev->card->codec;
2214 struct snd_soc_dapm_widget *w;
2215 LIST_HEAD(down_list);
2216 int powerdown = 0;
2217
2218 list_for_each_entry(w, &codec->dapm_widgets, list) {
2219 if (w->power) {
2220 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002221 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002222 powerdown = 1;
2223 }
2224 }
2225
2226 /* If there were no widgets to power down we're already in
2227 * standby.
2228 */
2229 if (powerdown) {
2230 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2231 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2232 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2233 }
2234
2235 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2236}
2237
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002239MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2241MODULE_LICENSE("GPL");