blob: 4cf58911f3b3bb9131dd67f7bae72931c01f30b1 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020041#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc-dapm.h>
45#include <sound/initval.h>
46
47/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010048#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020050#else
51#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020052#endif
53
Richard Purdie2b97eab2006-10-06 18:32:18 +020054/* dapm power sequences - make this per codec in the future */
55static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_pre] = 0,
57 [snd_soc_dapm_supply] = 1,
58 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
63 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
69 [snd_soc_dapm_hp] = 10,
70 [snd_soc_dapm_spk] = 10,
71 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020072};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000073
Richard Purdie2b97eab2006-10-06 18:32:18 +020074static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010075 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010078 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010087 [snd_soc_dapm_aif_in] = 10,
88 [snd_soc_dapm_aif_out] = 10,
89 [snd_soc_dapm_supply] = 11,
90 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020091};
92
Troy Kisky12ef1932008-10-13 17:42:14 -070093static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010094{
95 if (pop_time)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97}
98
Troy Kisky12ef1932008-10-13 17:42:14 -070099static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100100{
101 va_list args;
102
103 va_start(args, fmt);
104
105 if (pop_time) {
106 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700107 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100108 }
109
110 va_end(args);
111}
112
Richard Purdie2b97eab2006-10-06 18:32:18 +0200113/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100114static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200115 const struct snd_soc_dapm_widget *_widget)
116{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100117 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200118}
119
Mark Brown452c5ea2009-05-17 21:41:23 +0100120/**
121 * snd_soc_dapm_set_bias_level - set the bias level for the system
122 * @socdev: audio device
123 * @level: level to configure
124 *
125 * Configure the bias (power) levels for the SoC audio device.
126 *
127 * Returns 0 for success else error.
128 */
129static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
130 enum snd_soc_bias_level level)
131{
132 struct snd_soc_card *card = socdev->card;
133 struct snd_soc_codec *codec = socdev->card->codec;
134 int ret = 0;
135
Mark Brownf83fba82009-05-18 15:44:43 +0100136 switch (level) {
137 case SND_SOC_BIAS_ON:
138 dev_dbg(socdev->dev, "Setting full bias\n");
139 break;
140 case SND_SOC_BIAS_PREPARE:
141 dev_dbg(socdev->dev, "Setting bias prepare\n");
142 break;
143 case SND_SOC_BIAS_STANDBY:
144 dev_dbg(socdev->dev, "Setting standby bias\n");
145 break;
146 case SND_SOC_BIAS_OFF:
147 dev_dbg(socdev->dev, "Setting bias off\n");
148 break;
149 default:
150 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
151 return -EINVAL;
152 }
153
Mark Brown452c5ea2009-05-17 21:41:23 +0100154 if (card->set_bias_level)
155 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100156 if (ret == 0) {
157 if (codec->set_bias_level)
158 ret = codec->set_bias_level(codec, level);
159 else
160 codec->bias_level = level;
161 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100162
163 return ret;
164}
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* set up initial codec paths */
167static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
168 struct snd_soc_dapm_path *p, int i)
169{
170 switch (w->id) {
171 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000172 case snd_soc_dapm_mixer:
173 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200174 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100175 struct soc_mixer_control *mc = (struct soc_mixer_control *)
176 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400177 unsigned int reg = mc->reg;
178 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100179 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400180 unsigned int mask = (1 << fls(max)) - 1;
181 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200182
183 val = snd_soc_read(w->codec, reg);
184 val = (val >> shift) & mask;
185
186 if ((invert && !val) || (!invert && val))
187 p->connect = 1;
188 else
189 p->connect = 0;
190 }
191 break;
192 case snd_soc_dapm_mux: {
193 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
194 int val, item, bitmask;
195
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100196 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200197 ;
198 val = snd_soc_read(w->codec, e->reg);
199 item = (val >> e->shift_l) & (bitmask - 1);
200
201 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100202 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200203 if (!(strcmp(p->name, e->texts[i])) && item == i)
204 p->connect = 1;
205 }
206 }
207 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200208 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200209 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200210 w->kcontrols[i].private_value;
211 int val, item;
212
213 val = snd_soc_read(w->codec, e->reg);
214 val = (val >> e->shift_l) & e->mask;
215 for (item = 0; item < e->max; item++) {
216 if (val == e->values[item])
217 break;
218 }
219
220 p->connect = 0;
221 for (i = 0; i < e->max; i++) {
222 if (!(strcmp(p->name, e->texts[i])) && item == i)
223 p->connect = 1;
224 }
225 }
226 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200227 /* does not effect routing - always connected */
228 case snd_soc_dapm_pga:
229 case snd_soc_dapm_output:
230 case snd_soc_dapm_adc:
231 case snd_soc_dapm_input:
232 case snd_soc_dapm_dac:
233 case snd_soc_dapm_micbias:
234 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100235 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100236 case snd_soc_dapm_aif_in:
237 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200238 p->connect = 1;
239 break;
240 /* does effect routing - dynamically connected */
241 case snd_soc_dapm_hp:
242 case snd_soc_dapm_mic:
243 case snd_soc_dapm_spk:
244 case snd_soc_dapm_line:
245 case snd_soc_dapm_pre:
246 case snd_soc_dapm_post:
247 p->connect = 0;
248 break;
249 }
250}
251
Mark Brown74b8f952009-06-06 11:26:15 +0100252/* connect mux widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200253static int dapm_connect_mux(struct snd_soc_codec *codec,
254 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
255 struct snd_soc_dapm_path *path, const char *control_name,
256 const struct snd_kcontrol_new *kcontrol)
257{
258 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
259 int i;
260
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100261 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200262 if (!(strcmp(control_name, e->texts[i]))) {
263 list_add(&path->list, &codec->dapm_paths);
264 list_add(&path->list_sink, &dest->sources);
265 list_add(&path->list_source, &src->sinks);
266 path->name = (char*)e->texts[i];
267 dapm_set_path_status(dest, path, 0);
268 return 0;
269 }
270 }
271
272 return -ENODEV;
273}
274
Mark Brown74b8f952009-06-06 11:26:15 +0100275/* connect mixer widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276static int dapm_connect_mixer(struct snd_soc_codec *codec,
277 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
278 struct snd_soc_dapm_path *path, const char *control_name)
279{
280 int i;
281
282 /* search for mixer kcontrol */
283 for (i = 0; i < dest->num_kcontrols; i++) {
284 if (!strcmp(control_name, dest->kcontrols[i].name)) {
285 list_add(&path->list, &codec->dapm_paths);
286 list_add(&path->list_sink, &dest->sources);
287 list_add(&path->list_source, &src->sinks);
288 path->name = dest->kcontrols[i].name;
289 dapm_set_path_status(dest, path, i);
290 return 0;
291 }
292 }
293 return -ENODEV;
294}
295
296/* update dapm codec register bits */
297static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
298{
299 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300300 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301 struct snd_soc_codec *codec = widget->codec;
302
303 /* check for valid widgets */
304 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
305 widget->id == snd_soc_dapm_output ||
306 widget->id == snd_soc_dapm_hp ||
307 widget->id == snd_soc_dapm_mic ||
308 widget->id == snd_soc_dapm_line ||
309 widget->id == snd_soc_dapm_spk)
310 return 0;
311
312 power = widget->power;
313 if (widget->invert)
314 power = (power ? 0:1);
315
316 old = snd_soc_read(codec, widget->reg);
317 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
318
319 change = old != new;
320 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700321 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
322 widget->name, widget->power ? "on" : "off",
323 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200324 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700325 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200326 }
Mark Brownc1286b82008-07-07 19:26:03 +0100327 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
328 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 return change;
330}
331
332/* ramps the volume up or down to minimise pops before or after a
333 * DAPM power event */
334static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
335{
336 const struct snd_kcontrol_new *k = widget->kcontrols;
337
338 if (widget->muted && !power)
339 return 0;
340 if (!widget->muted && power)
341 return 0;
342
343 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100344 struct soc_mixer_control *mc =
345 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400346 unsigned int reg = mc->reg;
347 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100348 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400349 unsigned int mask = (1 << fls(max)) - 1;
350 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351
352 if (power) {
353 int i;
354 /* power up has happended, increase volume to last level */
355 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100356 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 snd_soc_update_bits(widget->codec, reg, mask, i);
358 } else {
359 for (i = 0; i < widget->saved_value; i++)
360 snd_soc_update_bits(widget->codec, reg, mask, i);
361 }
362 widget->muted = 0;
363 } else {
364 /* power down is about to occur, decrease volume to mute */
365 int val = snd_soc_read(widget->codec, reg);
366 int i = widget->saved_value = (val >> shift) & mask;
367 if (invert) {
368 for (; i < mask; i++)
369 snd_soc_update_bits(widget->codec, reg, mask, i);
370 } else {
371 for (; i > 0; i--)
372 snd_soc_update_bits(widget->codec, reg, mask, i);
373 }
374 widget->muted = 1;
375 }
376 }
377 return 0;
378}
379
380/* create new dapm mixer control */
381static int dapm_new_mixer(struct snd_soc_codec *codec,
382 struct snd_soc_dapm_widget *w)
383{
384 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000385 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386 struct snd_soc_dapm_path *path;
387
388 /* add kcontrol */
389 for (i = 0; i < w->num_kcontrols; i++) {
390
391 /* match name */
392 list_for_each_entry(path, &w->sources, list_sink) {
393
394 /* mixer/mux paths name must match control name */
395 if (path->name != (char*)w->kcontrols[i].name)
396 continue;
397
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 /* add dapm control with long name.
399 * for dapm_mixer this is the concatenation of the
400 * mixer and kcontrol name.
401 * for dapm_mixer_named_ctl this is simply the
402 * kcontrol name.
403 */
404 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000405 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000406 name_len += 1 + strlen(w->name);
407
Mark Brown219b93f2008-10-28 13:02:31 +0000408 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000409
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 if (path->long_name == NULL)
411 return -ENOMEM;
412
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000413 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000414 default:
415 snprintf(path->long_name, name_len, "%s %s",
416 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000417 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000418 case snd_soc_dapm_mixer_named_ctl:
419 snprintf(path->long_name, name_len, "%s",
420 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000421 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000422 }
423
Mark Brown219b93f2008-10-28 13:02:31 +0000424 path->long_name[name_len - 1] = '\0';
425
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
427 path->long_name);
428 ret = snd_ctl_add(codec->card, path->kcontrol);
429 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100430 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
431 path->long_name,
432 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433 kfree(path->long_name);
434 path->long_name = NULL;
435 return ret;
436 }
437 }
438 }
439 return ret;
440}
441
442/* create new dapm mux control */
443static int dapm_new_mux(struct snd_soc_codec *codec,
444 struct snd_soc_dapm_widget *w)
445{
446 struct snd_soc_dapm_path *path = NULL;
447 struct snd_kcontrol *kcontrol;
448 int ret = 0;
449
450 if (!w->num_kcontrols) {
451 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
452 return -EINVAL;
453 }
454
455 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
456 ret = snd_ctl_add(codec->card, kcontrol);
457 if (ret < 0)
458 goto err;
459
460 list_for_each_entry(path, &w->sources, list_sink)
461 path->kcontrol = kcontrol;
462
463 return ret;
464
465err:
466 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
467 return ret;
468}
469
470/* create new dapm volume control */
471static int dapm_new_pga(struct snd_soc_codec *codec,
472 struct snd_soc_dapm_widget *w)
473{
474 struct snd_kcontrol *kcontrol;
475 int ret = 0;
476
477 if (!w->num_kcontrols)
478 return -EINVAL;
479
480 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
481 ret = snd_ctl_add(codec->card, kcontrol);
482 if (ret < 0) {
483 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
484 return ret;
485 }
486
487 return ret;
488}
489
490/* reset 'walked' bit for each dapm path */
491static inline void dapm_clear_walk(struct snd_soc_codec *codec)
492{
493 struct snd_soc_dapm_path *p;
494
495 list_for_each_entry(p, &codec->dapm_paths, list)
496 p->walked = 0;
497}
498
499/*
500 * Recursively check for a completed path to an active or physically connected
501 * output widget. Returns number of complete paths.
502 */
503static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
504{
505 struct snd_soc_dapm_path *path;
506 int con = 0;
507
Mark Brown246d0a12009-04-22 18:24:55 +0100508 if (widget->id == snd_soc_dapm_supply)
509 return 0;
510
Mark Brown010ff262009-08-17 17:39:22 +0100511 switch (widget->id) {
512 case snd_soc_dapm_adc:
513 case snd_soc_dapm_aif_out:
514 if (widget->active)
515 return 1;
516 default:
517 break;
518 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200519
520 if (widget->connected) {
521 /* connected pin ? */
522 if (widget->id == snd_soc_dapm_output && !widget->ext)
523 return 1;
524
525 /* connected jack or spk ? */
526 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300527 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528 return 1;
529 }
530
531 list_for_each_entry(path, &widget->sinks, list_source) {
532 if (path->walked)
533 continue;
534
535 if (path->sink && path->connect) {
536 path->walked = 1;
537 con += is_connected_output_ep(path->sink);
538 }
539 }
540
541 return con;
542}
543
544/*
545 * Recursively check for a completed path to an active or physically connected
546 * input widget. Returns number of complete paths.
547 */
548static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
549{
550 struct snd_soc_dapm_path *path;
551 int con = 0;
552
Mark Brown246d0a12009-04-22 18:24:55 +0100553 if (widget->id == snd_soc_dapm_supply)
554 return 0;
555
Richard Purdie2b97eab2006-10-06 18:32:18 +0200556 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100557 switch (widget->id) {
558 case snd_soc_dapm_dac:
559 case snd_soc_dapm_aif_in:
560 if (widget->active)
561 return 1;
562 default:
563 break;
564 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565
566 if (widget->connected) {
567 /* connected pin ? */
568 if (widget->id == snd_soc_dapm_input && !widget->ext)
569 return 1;
570
571 /* connected VMID/Bias for lower pops */
572 if (widget->id == snd_soc_dapm_vmid)
573 return 1;
574
575 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300576 if (widget->id == snd_soc_dapm_mic ||
577 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 return 1;
579 }
580
581 list_for_each_entry(path, &widget->sources, list_sink) {
582 if (path->walked)
583 continue;
584
585 if (path->source && path->connect) {
586 path->walked = 1;
587 con += is_connected_input_ep(path->source);
588 }
589 }
590
591 return con;
592}
593
594/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300595 * Handler for generic register modifier widget.
596 */
597int dapm_reg_event(struct snd_soc_dapm_widget *w,
598 struct snd_kcontrol *kcontrol, int event)
599{
600 unsigned int val;
601
602 if (SND_SOC_DAPM_EVENT_ON(event))
603 val = w->on_val;
604 else
605 val = w->off_val;
606
607 snd_soc_update_bits(w->codec, -(w->reg + 1),
608 w->mask << w->shift, val << w->shift);
609
610 return 0;
611}
Mark Brown11589412008-07-29 11:42:23 +0100612EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300613
Mark Brown025756e2009-04-13 11:09:18 +0100614/* Standard power change method, used to apply power changes to most
615 * widgets.
616 */
617static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
618{
619 int ret;
620
621 /* call any power change event handlers */
622 if (w->event)
623 pr_debug("power %s event for %s flags %x\n",
624 w->power ? "on" : "off",
625 w->name, w->event_flags);
626
627 /* power up pre event */
628 if (w->power && w->event &&
629 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
630 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
631 if (ret < 0)
632 return ret;
633 }
634
635 /* power down pre event */
636 if (!w->power && w->event &&
637 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
638 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
639 if (ret < 0)
640 return ret;
641 }
642
643 /* Lower PGA volume to reduce pops */
644 if (w->id == snd_soc_dapm_pga && !w->power)
645 dapm_set_pga(w, w->power);
646
647 dapm_update_bits(w);
648
649 /* Raise PGA volume to reduce pops */
650 if (w->id == snd_soc_dapm_pga && w->power)
651 dapm_set_pga(w, w->power);
652
653 /* power up post event */
654 if (w->power && w->event &&
655 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
656 ret = w->event(w,
657 NULL, SND_SOC_DAPM_POST_PMU);
658 if (ret < 0)
659 return ret;
660 }
661
662 /* power down post event */
663 if (!w->power && w->event &&
664 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
665 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
666 if (ret < 0)
667 return ret;
668 }
669
670 return 0;
671}
672
Mark Browncd0f2d42009-04-20 16:56:59 +0100673/* Generic check to see if a widget should be powered.
674 */
675static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
676{
677 int in, out;
678
679 in = is_connected_input_ep(w);
680 dapm_clear_walk(w->codec);
681 out = is_connected_output_ep(w);
682 dapm_clear_walk(w->codec);
683 return out != 0 && in != 0;
684}
685
Mark Brown6ea31b92009-04-20 17:15:41 +0100686/* Check to see if an ADC has power */
687static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
688{
689 int in;
690
691 if (w->active) {
692 in = is_connected_input_ep(w);
693 dapm_clear_walk(w->codec);
694 return in != 0;
695 } else {
696 return dapm_generic_check_power(w);
697 }
698}
699
700/* Check to see if a DAC has power */
701static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
702{
703 int out;
704
705 if (w->active) {
706 out = is_connected_output_ep(w);
707 dapm_clear_walk(w->codec);
708 return out != 0;
709 } else {
710 return dapm_generic_check_power(w);
711 }
712}
713
Mark Brown246d0a12009-04-22 18:24:55 +0100714/* Check to see if a power supply is needed */
715static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
716{
717 struct snd_soc_dapm_path *path;
718 int power = 0;
719
720 /* Check if one of our outputs is connected */
721 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100722 if (path->connected &&
723 !path->connected(path->source, path->sink))
724 continue;
725
Mark Brown246d0a12009-04-22 18:24:55 +0100726 if (path->sink && path->sink->power_check &&
727 path->sink->power_check(path->sink)) {
728 power = 1;
729 break;
730 }
731 }
732
733 dapm_clear_walk(w->codec);
734
735 return power;
736}
737
Mark Brown38357ab2009-06-06 19:03:23 +0100738static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
739 struct snd_soc_dapm_widget *b,
740 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000741{
Mark Brown38357ab2009-06-06 19:03:23 +0100742 if (sort[a->id] != sort[b->id])
743 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100744 if (a->reg != b->reg)
745 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000746
Mark Brown38357ab2009-06-06 19:03:23 +0100747 return 0;
748}
Mark Brown42aa3412009-03-01 19:21:10 +0000749
Mark Brown38357ab2009-06-06 19:03:23 +0100750/* Insert a widget in order into a DAPM power sequence. */
751static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
752 struct list_head *list,
753 int sort[])
754{
755 struct snd_soc_dapm_widget *w;
756
757 list_for_each_entry(w, list, power_list)
758 if (dapm_seq_compare(new_widget, w, sort) < 0) {
759 list_add_tail(&new_widget->power_list, &w->power_list);
760 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000761 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100762
Mark Brown38357ab2009-06-06 19:03:23 +0100763 list_add_tail(&new_widget->power_list, list);
764}
Mark Brown42aa3412009-03-01 19:21:10 +0000765
Mark Brownb22ead22009-06-07 12:51:26 +0100766/* Apply the coalesced changes from a DAPM sequence */
767static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
768 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100769{
770 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100771 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100772 unsigned int value = 0;
773 unsigned int mask = 0;
774 unsigned int cur_mask;
775
776 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
777 power_list)->reg;
778
779 list_for_each_entry(w, pending, power_list) {
780 cur_mask = 1 << w->shift;
781 BUG_ON(reg != w->reg);
782
783 if (w->invert)
784 power = !w->power;
785 else
786 power = w->power;
787
788 mask |= cur_mask;
789 if (power)
790 value |= cur_mask;
791
792 pop_dbg(codec->pop_time,
793 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
794 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100795
796 /* power up pre event */
797 if (w->power && w->event &&
798 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
799 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
800 w->name);
801 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
802 if (ret < 0)
803 pr_err("%s: pre event failed: %d\n",
804 w->name, ret);
805 }
806
807 /* power down pre event */
808 if (!w->power && w->event &&
809 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
810 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
811 w->name);
812 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
813 if (ret < 0)
814 pr_err("%s: pre event failed: %d\n",
815 w->name, ret);
816 }
Mark Brown4f1c1922009-06-07 13:37:44 +0100817
818 /* Lower PGA volume to reduce pops */
819 if (w->id == snd_soc_dapm_pga && !w->power)
820 dapm_set_pga(w, w->power);
Mark Brownb22ead22009-06-07 12:51:26 +0100821 }
822
Mark Brown81628102009-06-07 13:21:24 +0100823 if (reg >= 0) {
824 pop_dbg(codec->pop_time,
825 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
826 value, mask, reg, codec->pop_time);
827 pop_wait(codec->pop_time);
828 snd_soc_update_bits(codec, reg, mask, value);
829 }
830
831 list_for_each_entry(w, pending, power_list) {
Mark Brown4f1c1922009-06-07 13:37:44 +0100832 /* Raise PGA volume to reduce pops */
833 if (w->id == snd_soc_dapm_pga && w->power)
834 dapm_set_pga(w, w->power);
835
Mark Brown81628102009-06-07 13:21:24 +0100836 /* power up post event */
837 if (w->power && w->event &&
838 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
839 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
840 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000841 ret = w->event(w,
842 NULL, SND_SOC_DAPM_POST_PMU);
843 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100844 pr_err("%s: post event failed: %d\n",
845 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000846 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100847
Mark Brown81628102009-06-07 13:21:24 +0100848 /* power down post event */
849 if (!w->power && w->event &&
850 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
851 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
852 w->name);
853 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
854 if (ret < 0)
855 pr_err("%s: post event failed: %d\n",
856 w->name, ret);
857 }
Mark Brown42aa3412009-03-01 19:21:10 +0000858 }
Mark Brown42aa3412009-03-01 19:21:10 +0000859}
860
Mark Brownb22ead22009-06-07 12:51:26 +0100861/* Apply a DAPM power sequence.
862 *
863 * We walk over a pre-sorted list of widgets to apply power to. In
864 * order to minimise the number of writes to the device required
865 * multiple widgets will be updated in a single write where possible.
866 * Currently anything that requires more than a single write is not
867 * handled.
868 */
869static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
870 int event, int sort[])
871{
872 struct snd_soc_dapm_widget *w, *n;
873 LIST_HEAD(pending);
874 int cur_sort = -1;
875 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100876 int ret;
877
Mark Brownb22ead22009-06-07 12:51:26 +0100878 list_for_each_entry_safe(w, n, list, power_list) {
879 ret = 0;
880
881 /* Do we need to apply any queued changes? */
882 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
883 if (!list_empty(&pending))
884 dapm_seq_run_coalesced(codec, &pending);
885
886 INIT_LIST_HEAD(&pending);
887 cur_sort = -1;
888 cur_reg = SND_SOC_NOPM;
889 }
890
Mark Brown163cac02009-06-07 10:12:52 +0100891 switch (w->id) {
892 case snd_soc_dapm_pre:
893 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100894 list_for_each_entry_safe_continue(w, n, list,
895 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100896
Mark Brownb22ead22009-06-07 12:51:26 +0100897 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100898 ret = w->event(w,
899 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100900 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100901 ret = w->event(w,
902 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100903 break;
904
905 case snd_soc_dapm_post:
906 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100907 list_for_each_entry_safe_continue(w, n, list,
908 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100909
Mark Brownb22ead22009-06-07 12:51:26 +0100910 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100911 ret = w->event(w,
912 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100913 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100914 ret = w->event(w,
915 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100916 break;
917
918 case snd_soc_dapm_input:
919 case snd_soc_dapm_output:
920 case snd_soc_dapm_hp:
921 case snd_soc_dapm_mic:
922 case snd_soc_dapm_line:
923 case snd_soc_dapm_spk:
924 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100925 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100926 break;
927
928 default:
Mark Brown81628102009-06-07 13:21:24 +0100929 /* Queue it up for application */
930 cur_sort = sort[w->id];
931 cur_reg = w->reg;
932 list_move(&w->power_list, &pending);
933 break;
Mark Brown163cac02009-06-07 10:12:52 +0100934 }
Mark Brownb22ead22009-06-07 12:51:26 +0100935
936 if (ret < 0)
937 pr_err("Failed to apply widget power: %d\n",
938 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100939 }
Mark Brownb22ead22009-06-07 12:51:26 +0100940
941 if (!list_empty(&pending))
942 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100943}
944
Mark Brown42aa3412009-03-01 19:21:10 +0000945/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200946 * Scan each dapm widget for complete audio path.
947 * A complete path is a route that has valid endpoints i.e.:-
948 *
949 * o DAC to output pin.
950 * o Input Pin to ADC.
951 * o Input pin to Output pin (bypass, sidetone)
952 * o DAC to ADC (loopback).
953 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100954static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955{
Mark Brown452c5ea2009-05-17 21:41:23 +0100956 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100958 LIST_HEAD(up_list);
959 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100960 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100961 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100962 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200963
Mark Brown6d3ddc82009-05-16 17:47:29 +0100964 /* Check which widgets we need to power and store them in
965 * lists indicating if they should be powered up or down.
966 */
967 list_for_each_entry(w, &codec->dapm_widgets, list) {
968 switch (w->id) {
969 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100970 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100971 break;
972 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100973 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100974 break;
975
976 default:
977 if (!w->power_check)
978 continue;
979
Mark Brown50b6bce2009-11-23 13:11:53 +0000980 /* If we're suspending then pull down all the
981 * power. */
982 switch (event) {
983 case SND_SOC_DAPM_STREAM_SUSPEND:
984 power = 0;
985 break;
986
987 default:
988 power = w->power_check(w);
989 if (power)
990 sys_power = 1;
991 break;
992 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100993
Mark Brown6d3ddc82009-05-16 17:47:29 +0100994 if (w->power == power)
995 continue;
996
997 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100998 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100999 else
Mark Brown291f3bb2009-06-07 13:57:17 +01001000 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001001
1002 w->power = power;
1003 break;
1004 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005 }
1006
Mark Brownb14b76a2009-08-17 11:55:38 +01001007 /* If there are no DAPM widgets then try to figure out power from the
1008 * event type.
1009 */
1010 if (list_empty(&codec->dapm_widgets)) {
1011 switch (event) {
1012 case SND_SOC_DAPM_STREAM_START:
1013 case SND_SOC_DAPM_STREAM_RESUME:
1014 sys_power = 1;
1015 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001016 case SND_SOC_DAPM_STREAM_SUSPEND:
1017 sys_power = 0;
1018 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001019 case SND_SOC_DAPM_STREAM_NOP:
1020 sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001021 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001022 default:
1023 break;
1024 }
1025 }
1026
Mark Brown452c5ea2009-05-17 21:41:23 +01001027 /* If we're changing to all on or all off then prepare */
1028 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
1029 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
1030 ret = snd_soc_dapm_set_bias_level(socdev,
1031 SND_SOC_BIAS_PREPARE);
1032 if (ret != 0)
1033 pr_err("Failed to prepare bias: %d\n", ret);
1034 }
1035
Mark Brown6d3ddc82009-05-16 17:47:29 +01001036 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001037 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001038
1039 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +01001040 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001041
Mark Brown452c5ea2009-05-17 21:41:23 +01001042 /* If we just powered the last thing off drop to standby bias */
1043 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1044 ret = snd_soc_dapm_set_bias_level(socdev,
1045 SND_SOC_BIAS_STANDBY);
1046 if (ret != 0)
1047 pr_err("Failed to apply standby bias: %d\n", ret);
1048 }
1049
1050 /* If we just powered up then move to active bias */
1051 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1052 ret = snd_soc_dapm_set_bias_level(socdev,
1053 SND_SOC_BIAS_ON);
1054 if (ret != 0)
1055 pr_err("Failed to apply active bias: %d\n", ret);
1056 }
1057
Mark Browncb507e72009-07-08 18:54:57 +01001058 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1059 codec->pop_time);
1060
Mark Brown42aa3412009-03-01 19:21:10 +00001061 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001062}
1063
Mark Brownc1286b82008-07-07 19:26:03 +01001064#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +02001065static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
1066{
1067 struct snd_soc_dapm_widget *w;
1068 struct snd_soc_dapm_path *p = NULL;
1069 int in, out;
1070
1071 printk("DAPM %s %s\n", codec->name, action);
1072
1073 list_for_each_entry(w, &codec->dapm_widgets, list) {
1074
1075 /* only display widgets that effect routing */
1076 switch (w->id) {
1077 case snd_soc_dapm_pre:
1078 case snd_soc_dapm_post:
1079 case snd_soc_dapm_vmid:
1080 continue;
1081 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001082 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001083 case snd_soc_dapm_output:
1084 case snd_soc_dapm_input:
1085 case snd_soc_dapm_switch:
1086 case snd_soc_dapm_hp:
1087 case snd_soc_dapm_mic:
1088 case snd_soc_dapm_spk:
1089 case snd_soc_dapm_line:
1090 case snd_soc_dapm_micbias:
1091 case snd_soc_dapm_dac:
1092 case snd_soc_dapm_adc:
1093 case snd_soc_dapm_pga:
1094 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001095 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001096 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001097 case snd_soc_dapm_aif_in:
1098 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001099 if (w->name) {
1100 in = is_connected_input_ep(w);
1101 dapm_clear_walk(w->codec);
1102 out = is_connected_output_ep(w);
1103 dapm_clear_walk(w->codec);
1104 printk("%s: %s in %d out %d\n", w->name,
1105 w->power ? "On":"Off",in, out);
1106
1107 list_for_each_entry(p, &w->sources, list_sink) {
1108 if (p->connect)
1109 printk(" in %s %s\n", p->name ? p->name : "static",
1110 p->source->name);
1111 }
1112 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001113 if (p->connect)
1114 printk(" out %s %s\n", p->name ? p->name : "static",
1115 p->sink->name);
1116 }
1117 }
1118 break;
1119 }
1120 }
1121}
1122#endif
1123
Mark Brown79fb9382009-08-21 16:38:13 +01001124#ifdef CONFIG_DEBUG_FS
1125static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1126{
1127 file->private_data = inode->i_private;
1128 return 0;
1129}
1130
1131static ssize_t dapm_widget_power_read_file(struct file *file,
1132 char __user *user_buf,
1133 size_t count, loff_t *ppos)
1134{
1135 struct snd_soc_dapm_widget *w = file->private_data;
1136 char *buf;
1137 int in, out;
1138 ssize_t ret;
1139 struct snd_soc_dapm_path *p = NULL;
1140
1141 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1142 if (!buf)
1143 return -ENOMEM;
1144
1145 in = is_connected_input_ep(w);
1146 dapm_clear_walk(w->codec);
1147 out = is_connected_output_ep(w);
1148 dapm_clear_walk(w->codec);
1149
Mark Brownd033c362009-12-04 15:25:56 +00001150 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001151 w->name, w->power ? "On" : "Off", in, out);
1152
Mark Brownd033c362009-12-04 15:25:56 +00001153 if (w->reg >= 0)
1154 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1155 " - R%d(0x%x) bit %d",
1156 w->reg, w->reg, w->shift);
1157
1158 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1159
Mark Brown3eef08b2009-09-14 16:49:00 +01001160 if (w->sname)
1161 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1162 w->sname,
1163 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001164
1165 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001166 if (p->connected && !p->connected(w, p->sink))
1167 continue;
1168
Mark Brown79fb9382009-08-21 16:38:13 +01001169 if (p->connect)
1170 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1171 " in %s %s\n",
1172 p->name ? p->name : "static",
1173 p->source->name);
1174 }
1175 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001176 if (p->connected && !p->connected(w, p->sink))
1177 continue;
1178
Mark Brown79fb9382009-08-21 16:38:13 +01001179 if (p->connect)
1180 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1181 " out %s %s\n",
1182 p->name ? p->name : "static",
1183 p->sink->name);
1184 }
1185
1186 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1187
1188 kfree(buf);
1189 return ret;
1190}
1191
1192static const struct file_operations dapm_widget_power_fops = {
1193 .open = dapm_widget_power_open_file,
1194 .read = dapm_widget_power_read_file,
1195};
1196
1197void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1198{
1199 struct snd_soc_dapm_widget *w;
1200 struct dentry *d;
1201
1202 if (!codec->debugfs_dapm)
1203 return;
1204
1205 list_for_each_entry(w, &codec->dapm_widgets, list) {
1206 if (!w->name)
1207 continue;
1208
1209 d = debugfs_create_file(w->name, 0444,
1210 codec->debugfs_dapm, w,
1211 &dapm_widget_power_fops);
1212 if (!d)
1213 printk(KERN_WARNING
1214 "ASoC: Failed to create %s debugfs file\n",
1215 w->name);
1216 }
1217}
1218#else
1219void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1220{
1221}
1222#endif
1223
Richard Purdie2b97eab2006-10-06 18:32:18 +02001224/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001225static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001226 struct snd_kcontrol *kcontrol, int change,
1227 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228{
1229 struct snd_soc_dapm_path *path;
1230 int found = 0;
1231
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001232 if (widget->id != snd_soc_dapm_mux &&
1233 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001234 return -ENODEV;
1235
Mark Brown3a655772009-10-05 17:23:30 +01001236 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001237 return 0;
1238
1239 /* find dapm widget path assoc with kcontrol */
1240 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1241 if (path->kcontrol != kcontrol)
1242 continue;
1243
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001244 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001245 continue;
1246
1247 found = 1;
1248 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001249 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250 path->connect = 1; /* new connection */
1251 else
1252 path->connect = 0; /* old connection must be powered down */
1253 }
1254
Mark Brown3fccd8b2008-07-07 19:26:04 +01001255 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001256 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001257 dump_dapm(widget->codec, "mux power update");
1258 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001259
1260 return 0;
1261}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001262
Milan plzik1b075e32008-01-10 14:39:46 +01001263/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001264static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001265 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266{
1267 struct snd_soc_dapm_path *path;
1268 int found = 0;
1269
Milan plzik1b075e32008-01-10 14:39:46 +01001270 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001271 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001272 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 return -ENODEV;
1274
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275 /* find dapm widget path assoc with kcontrol */
1276 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1277 if (path->kcontrol != kcontrol)
1278 continue;
1279
1280 /* found, now check type */
1281 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001282 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001283 break;
1284 }
1285
Mark Brown3fccd8b2008-07-07 19:26:04 +01001286 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001288 dump_dapm(widget->codec, "mixer power update");
1289 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001290
1291 return 0;
1292}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001293
1294/* show dapm widget status in sys fs */
1295static ssize_t dapm_widget_show(struct device *dev,
1296 struct device_attribute *attr, char *buf)
1297{
1298 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001299 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001300 struct snd_soc_dapm_widget *w;
1301 int count = 0;
1302 char *state = "not set";
1303
1304 list_for_each_entry(w, &codec->dapm_widgets, list) {
1305
1306 /* only display widgets that burnm power */
1307 switch (w->id) {
1308 case snd_soc_dapm_hp:
1309 case snd_soc_dapm_mic:
1310 case snd_soc_dapm_spk:
1311 case snd_soc_dapm_line:
1312 case snd_soc_dapm_micbias:
1313 case snd_soc_dapm_dac:
1314 case snd_soc_dapm_adc:
1315 case snd_soc_dapm_pga:
1316 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001317 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001318 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001319 if (w->name)
1320 count += sprintf(buf + count, "%s: %s\n",
1321 w->name, w->power ? "On":"Off");
1322 break;
1323 default:
1324 break;
1325 }
1326 }
1327
Mark Brown0be98982008-05-19 12:31:28 +02001328 switch (codec->bias_level) {
1329 case SND_SOC_BIAS_ON:
1330 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001331 break;
Mark Brown0be98982008-05-19 12:31:28 +02001332 case SND_SOC_BIAS_PREPARE:
1333 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 break;
Mark Brown0be98982008-05-19 12:31:28 +02001335 case SND_SOC_BIAS_STANDBY:
1336 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001337 break;
Mark Brown0be98982008-05-19 12:31:28 +02001338 case SND_SOC_BIAS_OFF:
1339 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001340 break;
1341 }
1342 count += sprintf(buf + count, "PM State: %s\n", state);
1343
1344 return count;
1345}
1346
1347static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1348
1349int snd_soc_dapm_sys_add(struct device *dev)
1350{
Troy Kisky12ef1932008-10-13 17:42:14 -07001351 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352}
1353
1354static void snd_soc_dapm_sys_remove(struct device *dev)
1355{
Mark Brownaef90842009-05-16 17:53:16 +01001356 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357}
1358
1359/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001360static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001361{
1362 struct snd_soc_dapm_widget *w, *next_w;
1363 struct snd_soc_dapm_path *p, *next_p;
1364
1365 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1366 list_del(&w->list);
1367 kfree(w);
1368 }
1369
1370 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1371 list_del(&p->list);
1372 kfree(p->long_name);
1373 kfree(p);
1374 }
1375}
1376
Liam Girdwooda5302182008-07-07 13:35:17 +01001377static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001378 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001379{
1380 struct snd_soc_dapm_widget *w;
1381
1382 list_for_each_entry(w, &codec->dapm_widgets, list) {
1383 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001384 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001385 w->connected = status;
1386 return 0;
1387 }
1388 }
1389
Mark Brownc1286b82008-07-07 19:26:03 +01001390 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001391 return -EINVAL;
1392}
1393
Richard Purdie2b97eab2006-10-06 18:32:18 +02001394/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001395 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001396 * @codec: audio codec
1397 *
1398 * Walks all dapm audio paths and powers widgets according to their
1399 * stream or path usage.
1400 *
1401 * Returns 0 for success.
1402 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001403int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001405 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1406 dump_dapm(codec, "sync");
1407 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001408}
Liam Girdwooda5302182008-07-07 13:35:17 +01001409EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001410
Mark Brown105f1c22008-05-13 14:52:19 +02001411static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001412 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413{
1414 struct snd_soc_dapm_path *path;
1415 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001416 const char *sink = route->sink;
1417 const char *control = route->control;
1418 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001419 int ret = 0;
1420
1421 /* find src and dest widgets */
1422 list_for_each_entry(w, &codec->dapm_widgets, list) {
1423
1424 if (!wsink && !(strcmp(w->name, sink))) {
1425 wsink = w;
1426 continue;
1427 }
1428 if (!wsource && !(strcmp(w->name, source))) {
1429 wsource = w;
1430 }
1431 }
1432
1433 if (wsource == NULL || wsink == NULL)
1434 return -ENODEV;
1435
1436 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1437 if (!path)
1438 return -ENOMEM;
1439
1440 path->source = wsource;
1441 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001442 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443 INIT_LIST_HEAD(&path->list);
1444 INIT_LIST_HEAD(&path->list_source);
1445 INIT_LIST_HEAD(&path->list_sink);
1446
1447 /* check for external widgets */
1448 if (wsink->id == snd_soc_dapm_input) {
1449 if (wsource->id == snd_soc_dapm_micbias ||
1450 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001451 wsource->id == snd_soc_dapm_line ||
1452 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001453 wsink->ext = 1;
1454 }
1455 if (wsource->id == snd_soc_dapm_output) {
1456 if (wsink->id == snd_soc_dapm_spk ||
1457 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001458 wsink->id == snd_soc_dapm_line ||
1459 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001460 wsource->ext = 1;
1461 }
1462
1463 /* connect static paths */
1464 if (control == NULL) {
1465 list_add(&path->list, &codec->dapm_paths);
1466 list_add(&path->list_sink, &wsink->sources);
1467 list_add(&path->list_source, &wsource->sinks);
1468 path->connect = 1;
1469 return 0;
1470 }
1471
1472 /* connect dynamic paths */
1473 switch(wsink->id) {
1474 case snd_soc_dapm_adc:
1475 case snd_soc_dapm_dac:
1476 case snd_soc_dapm_pga:
1477 case snd_soc_dapm_input:
1478 case snd_soc_dapm_output:
1479 case snd_soc_dapm_micbias:
1480 case snd_soc_dapm_vmid:
1481 case snd_soc_dapm_pre:
1482 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001483 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001484 case snd_soc_dapm_aif_in:
1485 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001486 list_add(&path->list, &codec->dapm_paths);
1487 list_add(&path->list_sink, &wsink->sources);
1488 list_add(&path->list_source, &wsource->sinks);
1489 path->connect = 1;
1490 return 0;
1491 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001492 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1494 &wsink->kcontrols[0]);
1495 if (ret != 0)
1496 goto err;
1497 break;
1498 case snd_soc_dapm_switch:
1499 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001500 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001501 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1502 if (ret != 0)
1503 goto err;
1504 break;
1505 case snd_soc_dapm_hp:
1506 case snd_soc_dapm_mic:
1507 case snd_soc_dapm_line:
1508 case snd_soc_dapm_spk:
1509 list_add(&path->list, &codec->dapm_paths);
1510 list_add(&path->list_sink, &wsink->sources);
1511 list_add(&path->list_source, &wsource->sinks);
1512 path->connect = 0;
1513 return 0;
1514 }
1515 return 0;
1516
1517err:
1518 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1519 control, sink);
1520 kfree(path);
1521 return ret;
1522}
Mark Brown105f1c22008-05-13 14:52:19 +02001523
1524/**
Mark Brown105f1c22008-05-13 14:52:19 +02001525 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1526 * @codec: codec
1527 * @route: audio routes
1528 * @num: number of routes
1529 *
1530 * Connects 2 dapm widgets together via a named audio path. The sink is
1531 * the widget receiving the audio signal, whilst the source is the sender
1532 * of the audio signal.
1533 *
1534 * Returns 0 for success else error. On error all resources can be freed
1535 * with a call to snd_soc_card_free().
1536 */
1537int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1538 const struct snd_soc_dapm_route *route, int num)
1539{
1540 int i, ret;
1541
1542 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001543 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001544 if (ret < 0) {
1545 printk(KERN_ERR "Failed to add route %s->%s\n",
1546 route->source,
1547 route->sink);
1548 return ret;
1549 }
1550 route++;
1551 }
1552
1553 return 0;
1554}
1555EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1556
1557/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 * snd_soc_dapm_new_widgets - add new dapm widgets
1559 * @codec: audio codec
1560 *
1561 * Checks the codec for any new dapm widgets and creates them if found.
1562 *
1563 * Returns 0 for success.
1564 */
1565int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1566{
1567 struct snd_soc_dapm_widget *w;
1568
Richard Purdie2b97eab2006-10-06 18:32:18 +02001569 list_for_each_entry(w, &codec->dapm_widgets, list)
1570 {
1571 if (w->new)
1572 continue;
1573
1574 switch(w->id) {
1575 case snd_soc_dapm_switch:
1576 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001577 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001578 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579 dapm_new_mixer(codec, w);
1580 break;
1581 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001582 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001583 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001584 dapm_new_mux(codec, w);
1585 break;
1586 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001587 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001588 w->power_check = dapm_adc_check_power;
1589 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001590 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001591 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001592 w->power_check = dapm_dac_check_power;
1593 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001594 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001595 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001596 dapm_new_pga(codec, w);
1597 break;
1598 case snd_soc_dapm_input:
1599 case snd_soc_dapm_output:
1600 case snd_soc_dapm_micbias:
1601 case snd_soc_dapm_spk:
1602 case snd_soc_dapm_hp:
1603 case snd_soc_dapm_mic:
1604 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001605 w->power_check = dapm_generic_check_power;
1606 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001607 case snd_soc_dapm_supply:
1608 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001609 case snd_soc_dapm_vmid:
1610 case snd_soc_dapm_pre:
1611 case snd_soc_dapm_post:
1612 break;
1613 }
1614 w->new = 1;
1615 }
1616
1617 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 return 0;
1619}
1620EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1621
1622/**
1623 * snd_soc_dapm_get_volsw - dapm mixer get callback
1624 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001625 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626 *
1627 * Callback to get the value of a dapm mixer control.
1628 *
1629 * Returns 0 for success.
1630 */
1631int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1632 struct snd_ctl_elem_value *ucontrol)
1633{
1634 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001635 struct soc_mixer_control *mc =
1636 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001637 unsigned int reg = mc->reg;
1638 unsigned int shift = mc->shift;
1639 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001640 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001641 unsigned int invert = mc->invert;
1642 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643
1644 /* return the saved value if we are powered down */
1645 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1646 ucontrol->value.integer.value[0] = widget->saved_value;
1647 return 0;
1648 }
1649
1650 ucontrol->value.integer.value[0] =
1651 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1652 if (shift != rshift)
1653 ucontrol->value.integer.value[1] =
1654 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1655 if (invert) {
1656 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001657 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 if (shift != rshift)
1659 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001660 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661 }
1662
1663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1666
1667/**
1668 * snd_soc_dapm_put_volsw - dapm mixer set callback
1669 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001670 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 *
1672 * Callback to set the value of a dapm mixer control.
1673 *
1674 * Returns 0 for success.
1675 */
1676int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol)
1678{
1679 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001680 struct soc_mixer_control *mc =
1681 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001682 unsigned int reg = mc->reg;
1683 unsigned int shift = mc->shift;
1684 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001685 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001686 unsigned int mask = (1 << fls(max)) - 1;
1687 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001688 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001689 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 int ret;
1691
1692 val = (ucontrol->value.integer.value[0] & mask);
1693
1694 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001695 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001696 val_mask = mask << shift;
1697 val = val << shift;
1698 if (shift != rshift) {
1699 val2 = (ucontrol->value.integer.value[1] & mask);
1700 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001701 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001702 val_mask |= mask << rshift;
1703 val |= val2 << rshift;
1704 }
1705
1706 mutex_lock(&widget->codec->mutex);
1707 widget->value = val;
1708
1709 /* save volume value if the widget is powered down */
1710 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1711 widget->saved_value = val;
1712 mutex_unlock(&widget->codec->mutex);
1713 return 1;
1714 }
1715
Mark Brown283375c2009-12-07 18:09:03 +00001716 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1717 if (val)
1718 /* new connection */
1719 connect = invert ? 0:1;
1720 else
1721 /* old connection must be powered down */
1722 connect = invert ? 1:0;
1723
1724 dapm_mixer_update_power(widget, kcontrol, connect);
1725 }
1726
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 if (widget->event) {
1728 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001729 ret = widget->event(widget, kcontrol,
1730 SND_SOC_DAPM_PRE_REG);
1731 if (ret < 0) {
1732 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001733 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001734 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 }
1736 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1737 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001738 ret = widget->event(widget, kcontrol,
1739 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 } else
1741 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1742
1743out:
1744 mutex_unlock(&widget->codec->mutex);
1745 return ret;
1746}
1747EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1748
1749/**
1750 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1751 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001752 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 *
1754 * Callback to get the value of a dapm enumerated double mixer control.
1755 *
1756 * Returns 0 for success.
1757 */
1758int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1759 struct snd_ctl_elem_value *ucontrol)
1760{
1761 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1762 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001763 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001765 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 ;
1767 val = snd_soc_read(widget->codec, e->reg);
1768 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1769 if (e->shift_l != e->shift_r)
1770 ucontrol->value.enumerated.item[1] =
1771 (val >> e->shift_r) & (bitmask - 1);
1772
1773 return 0;
1774}
1775EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1776
1777/**
1778 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1779 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001780 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 *
1782 * Callback to set the value of a dapm enumerated double mixer control.
1783 *
1784 * Returns 0 for success.
1785 */
1786int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_value *ucontrol)
1788{
1789 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1790 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001791 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001792 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 int ret = 0;
1794
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001795 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001796 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001797 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 return -EINVAL;
1799 mux = ucontrol->value.enumerated.item[0];
1800 val = mux << e->shift_l;
1801 mask = (bitmask - 1) << e->shift_l;
1802 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001803 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001804 return -EINVAL;
1805 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1806 mask |= (bitmask - 1) << e->shift_r;
1807 }
1808
1809 mutex_lock(&widget->codec->mutex);
1810 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001811 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1812 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001813
1814 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1815 ret = widget->event(widget,
1816 kcontrol, SND_SOC_DAPM_PRE_REG);
1817 if (ret < 0)
1818 goto out;
1819 }
1820
1821 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1822
1823 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1824 ret = widget->event(widget,
1825 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826
1827out:
1828 mutex_unlock(&widget->codec->mutex);
1829 return ret;
1830}
1831EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1832
1833/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001834 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1835 * @kcontrol: mixer control
1836 * @ucontrol: control element information
1837 *
1838 * Returns 0 for success.
1839 */
1840int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1841 struct snd_ctl_elem_value *ucontrol)
1842{
1843 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1844
1845 ucontrol->value.enumerated.item[0] = widget->value;
1846
1847 return 0;
1848}
1849EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1850
1851/**
1852 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1853 * @kcontrol: mixer control
1854 * @ucontrol: control element information
1855 *
1856 * Returns 0 for success.
1857 */
1858int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_value *ucontrol)
1860{
1861 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1862 struct soc_enum *e =
1863 (struct soc_enum *)kcontrol->private_value;
1864 int change;
1865 int ret = 0;
1866
1867 if (ucontrol->value.enumerated.item[0] >= e->max)
1868 return -EINVAL;
1869
1870 mutex_lock(&widget->codec->mutex);
1871
1872 change = widget->value != ucontrol->value.enumerated.item[0];
1873 widget->value = ucontrol->value.enumerated.item[0];
1874 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1875
1876 mutex_unlock(&widget->codec->mutex);
1877 return ret;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1880
1881/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001882 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1883 * callback
1884 * @kcontrol: mixer control
1885 * @ucontrol: control element information
1886 *
1887 * Callback to get the value of a dapm semi enumerated double mixer control.
1888 *
1889 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1890 * used for handling bitfield coded enumeration for example.
1891 *
1892 * Returns 0 for success.
1893 */
1894int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
1896{
1897 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001898 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001899 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001900
1901 reg_val = snd_soc_read(widget->codec, e->reg);
1902 val = (reg_val >> e->shift_l) & e->mask;
1903 for (mux = 0; mux < e->max; mux++) {
1904 if (val == e->values[mux])
1905 break;
1906 }
1907 ucontrol->value.enumerated.item[0] = mux;
1908 if (e->shift_l != e->shift_r) {
1909 val = (reg_val >> e->shift_r) & e->mask;
1910 for (mux = 0; mux < e->max; mux++) {
1911 if (val == e->values[mux])
1912 break;
1913 }
1914 ucontrol->value.enumerated.item[1] = mux;
1915 }
1916
1917 return 0;
1918}
1919EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1920
1921/**
1922 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1923 * callback
1924 * @kcontrol: mixer control
1925 * @ucontrol: control element information
1926 *
1927 * Callback to set the value of a dapm semi enumerated double mixer control.
1928 *
1929 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1930 * used for handling bitfield coded enumeration for example.
1931 *
1932 * Returns 0 for success.
1933 */
1934int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
1937 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001938 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001939 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001940 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001941 int ret = 0;
1942
1943 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1944 return -EINVAL;
1945 mux = ucontrol->value.enumerated.item[0];
1946 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1947 mask = e->mask << e->shift_l;
1948 if (e->shift_l != e->shift_r) {
1949 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1950 return -EINVAL;
1951 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1952 mask |= e->mask << e->shift_r;
1953 }
1954
1955 mutex_lock(&widget->codec->mutex);
1956 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001957 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1958 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001959
1960 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1961 ret = widget->event(widget,
1962 kcontrol, SND_SOC_DAPM_PRE_REG);
1963 if (ret < 0)
1964 goto out;
1965 }
1966
1967 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1968
1969 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1970 ret = widget->event(widget,
1971 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001972
1973out:
1974 mutex_unlock(&widget->codec->mutex);
1975 return ret;
1976}
1977EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1978
1979/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001980 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1981 *
1982 * @kcontrol: mixer control
1983 * @uinfo: control element information
1984 *
1985 * Callback to provide information about a pin switch control.
1986 */
1987int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_info *uinfo)
1989{
1990 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1991 uinfo->count = 1;
1992 uinfo->value.integer.min = 0;
1993 uinfo->value.integer.max = 1;
1994
1995 return 0;
1996}
1997EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1998
1999/**
2000 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2001 *
2002 * @kcontrol: mixer control
2003 * @ucontrol: Value
2004 */
2005int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_value *ucontrol)
2007{
2008 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2009 const char *pin = (const char *)kcontrol->private_value;
2010
2011 mutex_lock(&codec->mutex);
2012
2013 ucontrol->value.integer.value[0] =
2014 snd_soc_dapm_get_pin_status(codec, pin);
2015
2016 mutex_unlock(&codec->mutex);
2017
2018 return 0;
2019}
2020EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2021
2022/**
2023 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2024 *
2025 * @kcontrol: mixer control
2026 * @ucontrol: Value
2027 */
2028int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2029 struct snd_ctl_elem_value *ucontrol)
2030{
2031 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2032 const char *pin = (const char *)kcontrol->private_value;
2033
2034 mutex_lock(&codec->mutex);
2035
2036 if (ucontrol->value.integer.value[0])
2037 snd_soc_dapm_enable_pin(codec, pin);
2038 else
2039 snd_soc_dapm_disable_pin(codec, pin);
2040
2041 snd_soc_dapm_sync(codec);
2042
2043 mutex_unlock(&codec->mutex);
2044
2045 return 0;
2046}
2047EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2048
2049/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002050 * snd_soc_dapm_new_control - create new dapm control
2051 * @codec: audio codec
2052 * @widget: widget template
2053 *
2054 * Creates a new dapm control based upon the template.
2055 *
2056 * Returns 0 for success else error.
2057 */
2058int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
2059 const struct snd_soc_dapm_widget *widget)
2060{
2061 struct snd_soc_dapm_widget *w;
2062
2063 if ((w = dapm_cnew_widget(widget)) == NULL)
2064 return -ENOMEM;
2065
2066 w->codec = codec;
2067 INIT_LIST_HEAD(&w->sources);
2068 INIT_LIST_HEAD(&w->sinks);
2069 INIT_LIST_HEAD(&w->list);
2070 list_add(&w->list, &codec->dapm_widgets);
2071
2072 /* machine layer set ups unconnected pins and insertions */
2073 w->connected = 1;
2074 return 0;
2075}
2076EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2077
2078/**
Mark Brown4ba13272008-05-13 14:51:19 +02002079 * snd_soc_dapm_new_controls - create new dapm controls
2080 * @codec: audio codec
2081 * @widget: widget array
2082 * @num: number of widgets
2083 *
2084 * Creates new DAPM controls based upon the templates.
2085 *
2086 * Returns 0 for success else error.
2087 */
2088int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
2089 const struct snd_soc_dapm_widget *widget,
2090 int num)
2091{
2092 int i, ret;
2093
2094 for (i = 0; i < num; i++) {
2095 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002096 if (ret < 0) {
2097 printk(KERN_ERR
2098 "ASoC: Failed to create DAPM control %s: %d\n",
2099 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002100 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002101 }
Mark Brown4ba13272008-05-13 14:51:19 +02002102 widget++;
2103 }
2104 return 0;
2105}
2106EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2107
2108
2109/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2111 * @codec: audio codec
2112 * @stream: stream name
2113 * @event: stream event
2114 *
2115 * Sends a stream event to the dapm core. The core then makes any
2116 * necessary widget power changes.
2117 *
2118 * Returns 0 for success else error.
2119 */
2120int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2121 char *stream, int event)
2122{
2123 struct snd_soc_dapm_widget *w;
2124
Seth Forshee11da21a2007-02-02 17:14:19 +01002125 if (stream == NULL)
2126 return 0;
2127
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 mutex_lock(&codec->mutex);
2129 list_for_each_entry(w, &codec->dapm_widgets, list)
2130 {
2131 if (!w->sname)
2132 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002133 pr_debug("widget %s\n %s stream %s event %d\n",
2134 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 if (strstr(w->sname, stream)) {
2136 switch(event) {
2137 case SND_SOC_DAPM_STREAM_START:
2138 w->active = 1;
2139 break;
2140 case SND_SOC_DAPM_STREAM_STOP:
2141 w->active = 0;
2142 break;
2143 case SND_SOC_DAPM_STREAM_SUSPEND:
2144 if (w->active)
2145 w->suspend = 1;
2146 w->active = 0;
2147 break;
2148 case SND_SOC_DAPM_STREAM_RESUME:
2149 if (w->suspend) {
2150 w->active = 1;
2151 w->suspend = 0;
2152 }
2153 break;
2154 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2155 break;
2156 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2157 break;
2158 }
2159 }
2160 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161
2162 dapm_power_widgets(codec, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002163 mutex_unlock(&codec->mutex);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08002164 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165 return 0;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2168
2169/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002170 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002171 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002172 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173 *
Mark Brown74b8f952009-06-06 11:26:15 +01002174 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002175 * a valid audio route and active audio stream.
2176 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2177 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 */
Mark Brown16499232009-01-07 18:25:13 +00002179int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180{
Liam Girdwooda5302182008-07-07 13:35:17 +01002181 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182}
Liam Girdwooda5302182008-07-07 13:35:17 +01002183EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184
2185/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002186 * snd_soc_dapm_disable_pin - disable pin.
2187 * @codec: SoC codec
2188 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002189 *
Mark Brown74b8f952009-06-06 11:26:15 +01002190 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002191 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2192 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002193 */
Mark Brown16499232009-01-07 18:25:13 +00002194int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002195{
2196 return snd_soc_dapm_set_pin(codec, pin, 0);
2197}
2198EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2199
2200/**
Mark Brown5817b522008-09-24 11:23:11 +01002201 * snd_soc_dapm_nc_pin - permanently disable pin.
2202 * @codec: SoC codec
2203 * @pin: pin name
2204 *
2205 * Marks the specified pin as being not connected, disabling it along
2206 * any parent or child widgets. At present this is identical to
2207 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2208 * additional things such as disabling controls which only affect
2209 * paths through the pin.
2210 *
2211 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2212 * do any widget power switching.
2213 */
Mark Brown16499232009-01-07 18:25:13 +00002214int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002215{
2216 return snd_soc_dapm_set_pin(codec, pin, 0);
2217}
2218EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2219
2220/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002221 * snd_soc_dapm_get_pin_status - get audio pin status
2222 * @codec: audio codec
2223 * @pin: audio signal pin endpoint (or start point)
2224 *
2225 * Get audio pin status - connected or disconnected.
2226 *
2227 * Returns 1 for connected otherwise 0.
2228 */
Mark Brown16499232009-01-07 18:25:13 +00002229int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002230{
2231 struct snd_soc_dapm_widget *w;
2232
2233 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002234 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002235 return w->connected;
2236 }
2237
2238 return 0;
2239}
Liam Girdwooda5302182008-07-07 13:35:17 +01002240EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002241
2242/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 * snd_soc_dapm_free - free dapm resources
2244 * @socdev: SoC device
2245 *
2246 * Free all dapm widgets and resources.
2247 */
2248void snd_soc_dapm_free(struct snd_soc_device *socdev)
2249{
Mark Brown6627a652009-01-23 22:55:23 +00002250 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251
2252 snd_soc_dapm_sys_remove(socdev->dev);
2253 dapm_free_widgets(codec);
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2256
Mark Brown51737472009-06-22 13:16:51 +01002257/*
2258 * snd_soc_dapm_shutdown - callback for system shutdown
2259 */
2260void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2261{
2262 struct snd_soc_codec *codec = socdev->card->codec;
2263 struct snd_soc_dapm_widget *w;
2264 LIST_HEAD(down_list);
2265 int powerdown = 0;
2266
2267 list_for_each_entry(w, &codec->dapm_widgets, list) {
2268 if (w->power) {
2269 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002270 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002271 powerdown = 1;
2272 }
2273 }
2274
2275 /* If there were no widgets to power down we're already in
2276 * standby.
2277 */
2278 if (powerdown) {
2279 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2280 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2281 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2282 }
2283
2284 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2285}
2286
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002288MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2290MODULE_LICENSE("GPL");