blob: 03cb7c05ebec2f26800fd7dc35e4798a811fcea1 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/soc-dapm.h>
46#include <sound/initval.h>
47
Richard Purdie2b97eab2006-10-06 18:32:18 +020048/* dapm power sequences - make this per codec in the future */
49static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010050 [snd_soc_dapm_pre] = 0,
51 [snd_soc_dapm_supply] = 1,
52 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010053 [snd_soc_dapm_aif_in] = 3,
54 [snd_soc_dapm_aif_out] = 3,
55 [snd_soc_dapm_mic] = 4,
56 [snd_soc_dapm_mux] = 5,
57 [snd_soc_dapm_value_mux] = 5,
58 [snd_soc_dapm_dac] = 6,
59 [snd_soc_dapm_mixer] = 7,
60 [snd_soc_dapm_mixer_named_ctl] = 7,
61 [snd_soc_dapm_pga] = 8,
62 [snd_soc_dapm_adc] = 9,
63 [snd_soc_dapm_hp] = 10,
64 [snd_soc_dapm_spk] = 10,
65 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020066};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000067
Richard Purdie2b97eab2006-10-06 18:32:18 +020068static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010069 [snd_soc_dapm_pre] = 0,
70 [snd_soc_dapm_adc] = 1,
71 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010072 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pga] = 4,
74 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010075 [snd_soc_dapm_mixer] = 5,
76 [snd_soc_dapm_dac] = 6,
77 [snd_soc_dapm_mic] = 7,
78 [snd_soc_dapm_micbias] = 8,
79 [snd_soc_dapm_mux] = 9,
80 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010081 [snd_soc_dapm_aif_in] = 10,
82 [snd_soc_dapm_aif_out] = 10,
83 [snd_soc_dapm_supply] = 11,
84 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
86
Troy Kisky12ef1932008-10-13 17:42:14 -070087static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010088{
89 if (pop_time)
90 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
91}
92
Troy Kisky12ef1932008-10-13 17:42:14 -070093static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010094{
95 va_list args;
96
97 va_start(args, fmt);
98
99 if (pop_time) {
100 vprintk(fmt, args);
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);
Troy Kisky12ef1932008-10-13 17:42:14 -0700317 pop_wait(codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000318 snd_soc_write(codec, widget->reg, new);
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
Richard Purdie2b97eab2006-10-06 18:32:18 +0200325/* create new dapm mixer control */
326static int dapm_new_mixer(struct snd_soc_codec *codec,
327 struct snd_soc_dapm_widget *w)
328{
329 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000330 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200331 struct snd_soc_dapm_path *path;
332
333 /* add kcontrol */
334 for (i = 0; i < w->num_kcontrols; i++) {
335
336 /* match name */
337 list_for_each_entry(path, &w->sources, list_sink) {
338
339 /* mixer/mux paths name must match control name */
340 if (path->name != (char*)w->kcontrols[i].name)
341 continue;
342
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000343 /* add dapm control with long name.
344 * for dapm_mixer this is the concatenation of the
345 * mixer and kcontrol name.
346 * for dapm_mixer_named_ctl this is simply the
347 * kcontrol name.
348 */
349 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000350 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000351 name_len += 1 + strlen(w->name);
352
Mark Brown219b93f2008-10-28 13:02:31 +0000353 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000354
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 if (path->long_name == NULL)
356 return -ENOMEM;
357
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000358 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000359 default:
360 snprintf(path->long_name, name_len, "%s %s",
361 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000362 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000363 case snd_soc_dapm_mixer_named_ctl:
364 snprintf(path->long_name, name_len, "%s",
365 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000366 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000367 }
368
Mark Brown219b93f2008-10-28 13:02:31 +0000369 path->long_name[name_len - 1] = '\0';
370
Richard Purdie2b97eab2006-10-06 18:32:18 +0200371 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
372 path->long_name);
373 ret = snd_ctl_add(codec->card, path->kcontrol);
374 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100375 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
376 path->long_name,
377 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378 kfree(path->long_name);
379 path->long_name = NULL;
380 return ret;
381 }
382 }
383 }
384 return ret;
385}
386
387/* create new dapm mux control */
388static int dapm_new_mux(struct snd_soc_codec *codec,
389 struct snd_soc_dapm_widget *w)
390{
391 struct snd_soc_dapm_path *path = NULL;
392 struct snd_kcontrol *kcontrol;
393 int ret = 0;
394
395 if (!w->num_kcontrols) {
396 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
397 return -EINVAL;
398 }
399
400 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
401 ret = snd_ctl_add(codec->card, kcontrol);
402 if (ret < 0)
403 goto err;
404
405 list_for_each_entry(path, &w->sources, list_sink)
406 path->kcontrol = kcontrol;
407
408 return ret;
409
410err:
411 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
412 return ret;
413}
414
415/* create new dapm volume control */
416static int dapm_new_pga(struct snd_soc_codec *codec,
417 struct snd_soc_dapm_widget *w)
418{
Mark Browna6c65732010-03-03 17:45:21 +0000419 if (w->num_kcontrols)
420 pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421
Mark Browna6c65732010-03-03 17:45:21 +0000422 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423}
424
425/* reset 'walked' bit for each dapm path */
426static inline void dapm_clear_walk(struct snd_soc_codec *codec)
427{
428 struct snd_soc_dapm_path *p;
429
430 list_for_each_entry(p, &codec->dapm_paths, list)
431 p->walked = 0;
432}
433
Mark Brown99497882010-05-07 20:24:05 +0100434/* We implement power down on suspend by checking the power state of
435 * the ALSA card - when we are suspending the ALSA state for the card
436 * is set to D3.
437 */
438static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
439{
440 struct snd_soc_codec *codec = widget->codec;
441
442 switch (snd_power_get_state(codec->card)) {
443 case SNDRV_CTL_POWER_D3hot:
444 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100445 if (widget->ignore_suspend)
446 pr_debug("%s ignoring suspend\n", widget->name);
447 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100448 default:
449 return 1;
450 }
451}
452
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453/*
454 * Recursively check for a completed path to an active or physically connected
455 * output widget. Returns number of complete paths.
456 */
457static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
458{
459 struct snd_soc_dapm_path *path;
460 int con = 0;
461
Mark Brown246d0a12009-04-22 18:24:55 +0100462 if (widget->id == snd_soc_dapm_supply)
463 return 0;
464
Mark Brown010ff262009-08-17 17:39:22 +0100465 switch (widget->id) {
466 case snd_soc_dapm_adc:
467 case snd_soc_dapm_aif_out:
468 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100469 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100470 default:
471 break;
472 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200473
474 if (widget->connected) {
475 /* connected pin ? */
476 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100477 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478
479 /* connected jack or spk ? */
480 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300481 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100482 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200483 }
484
485 list_for_each_entry(path, &widget->sinks, list_source) {
486 if (path->walked)
487 continue;
488
489 if (path->sink && path->connect) {
490 path->walked = 1;
491 con += is_connected_output_ep(path->sink);
492 }
493 }
494
495 return con;
496}
497
498/*
499 * Recursively check for a completed path to an active or physically connected
500 * input widget. Returns number of complete paths.
501 */
502static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
503{
504 struct snd_soc_dapm_path *path;
505 int con = 0;
506
Mark Brown246d0a12009-04-22 18:24:55 +0100507 if (widget->id == snd_soc_dapm_supply)
508 return 0;
509
Richard Purdie2b97eab2006-10-06 18:32:18 +0200510 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100511 switch (widget->id) {
512 case snd_soc_dapm_dac:
513 case snd_soc_dapm_aif_in:
514 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100515 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100516 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_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100523 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200524
525 /* connected VMID/Bias for lower pops */
526 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100527 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528
529 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300530 if (widget->id == snd_soc_dapm_mic ||
531 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100532 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533 }
534
535 list_for_each_entry(path, &widget->sources, list_sink) {
536 if (path->walked)
537 continue;
538
539 if (path->source && path->connect) {
540 path->walked = 1;
541 con += is_connected_input_ep(path->source);
542 }
543 }
544
545 return con;
546}
547
548/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300549 * Handler for generic register modifier widget.
550 */
551int dapm_reg_event(struct snd_soc_dapm_widget *w,
552 struct snd_kcontrol *kcontrol, int event)
553{
554 unsigned int val;
555
556 if (SND_SOC_DAPM_EVENT_ON(event))
557 val = w->on_val;
558 else
559 val = w->off_val;
560
561 snd_soc_update_bits(w->codec, -(w->reg + 1),
562 w->mask << w->shift, val << w->shift);
563
564 return 0;
565}
Mark Brown11589412008-07-29 11:42:23 +0100566EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300567
Mark Brown025756e2009-04-13 11:09:18 +0100568/* Standard power change method, used to apply power changes to most
569 * widgets.
570 */
571static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
572{
573 int ret;
574
575 /* call any power change event handlers */
576 if (w->event)
577 pr_debug("power %s event for %s flags %x\n",
578 w->power ? "on" : "off",
579 w->name, w->event_flags);
580
581 /* power up pre event */
582 if (w->power && w->event &&
583 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
584 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
585 if (ret < 0)
586 return ret;
587 }
588
589 /* power down pre event */
590 if (!w->power && w->event &&
591 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
592 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
593 if (ret < 0)
594 return ret;
595 }
596
Mark Brown025756e2009-04-13 11:09:18 +0100597 dapm_update_bits(w);
598
Mark Brown025756e2009-04-13 11:09:18 +0100599 /* power up post event */
600 if (w->power && w->event &&
601 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
602 ret = w->event(w,
603 NULL, SND_SOC_DAPM_POST_PMU);
604 if (ret < 0)
605 return ret;
606 }
607
608 /* power down post event */
609 if (!w->power && w->event &&
610 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
611 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
612 if (ret < 0)
613 return ret;
614 }
615
616 return 0;
617}
618
Mark Browncd0f2d42009-04-20 16:56:59 +0100619/* Generic check to see if a widget should be powered.
620 */
621static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
622{
623 int in, out;
624
625 in = is_connected_input_ep(w);
626 dapm_clear_walk(w->codec);
627 out = is_connected_output_ep(w);
628 dapm_clear_walk(w->codec);
629 return out != 0 && in != 0;
630}
631
Mark Brown6ea31b92009-04-20 17:15:41 +0100632/* Check to see if an ADC has power */
633static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
634{
635 int in;
636
637 if (w->active) {
638 in = is_connected_input_ep(w);
639 dapm_clear_walk(w->codec);
640 return in != 0;
641 } else {
642 return dapm_generic_check_power(w);
643 }
644}
645
646/* Check to see if a DAC has power */
647static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
648{
649 int out;
650
651 if (w->active) {
652 out = is_connected_output_ep(w);
653 dapm_clear_walk(w->codec);
654 return out != 0;
655 } else {
656 return dapm_generic_check_power(w);
657 }
658}
659
Mark Brown246d0a12009-04-22 18:24:55 +0100660/* Check to see if a power supply is needed */
661static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
662{
663 struct snd_soc_dapm_path *path;
664 int power = 0;
665
666 /* Check if one of our outputs is connected */
667 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100668 if (path->connected &&
669 !path->connected(path->source, path->sink))
670 continue;
671
Mark Brown246d0a12009-04-22 18:24:55 +0100672 if (path->sink && path->sink->power_check &&
673 path->sink->power_check(path->sink)) {
674 power = 1;
675 break;
676 }
677 }
678
679 dapm_clear_walk(w->codec);
680
681 return power;
682}
683
Mark Brown38357ab2009-06-06 19:03:23 +0100684static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
685 struct snd_soc_dapm_widget *b,
686 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000687{
Mark Brownd207c682009-12-07 17:13:55 +0000688 if (a->codec != b->codec)
689 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100690 if (sort[a->id] != sort[b->id])
691 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100692 if (a->reg != b->reg)
693 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000694
Mark Brown38357ab2009-06-06 19:03:23 +0100695 return 0;
696}
Mark Brown42aa3412009-03-01 19:21:10 +0000697
Mark Brown38357ab2009-06-06 19:03:23 +0100698/* Insert a widget in order into a DAPM power sequence. */
699static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
700 struct list_head *list,
701 int sort[])
702{
703 struct snd_soc_dapm_widget *w;
704
705 list_for_each_entry(w, list, power_list)
706 if (dapm_seq_compare(new_widget, w, sort) < 0) {
707 list_add_tail(&new_widget->power_list, &w->power_list);
708 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000709 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100710
Mark Brown38357ab2009-06-06 19:03:23 +0100711 list_add_tail(&new_widget->power_list, list);
712}
Mark Brown42aa3412009-03-01 19:21:10 +0000713
Mark Brownb22ead22009-06-07 12:51:26 +0100714/* Apply the coalesced changes from a DAPM sequence */
715static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
716 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100717{
718 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100719 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100720 unsigned int value = 0;
721 unsigned int mask = 0;
722 unsigned int cur_mask;
723
724 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
725 power_list)->reg;
726
727 list_for_each_entry(w, pending, power_list) {
728 cur_mask = 1 << w->shift;
729 BUG_ON(reg != w->reg);
730
731 if (w->invert)
732 power = !w->power;
733 else
734 power = w->power;
735
736 mask |= cur_mask;
737 if (power)
738 value |= cur_mask;
739
740 pop_dbg(codec->pop_time,
741 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
742 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100743
744 /* power up pre event */
745 if (w->power && w->event &&
746 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
747 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
748 w->name);
749 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
750 if (ret < 0)
751 pr_err("%s: pre event failed: %d\n",
752 w->name, ret);
753 }
754
755 /* power down pre event */
756 if (!w->power && w->event &&
757 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
758 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
759 w->name);
760 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
761 if (ret < 0)
762 pr_err("%s: pre event failed: %d\n",
763 w->name, ret);
764 }
Mark Brownb22ead22009-06-07 12:51:26 +0100765 }
766
Mark Brown81628102009-06-07 13:21:24 +0100767 if (reg >= 0) {
768 pop_dbg(codec->pop_time,
769 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
770 value, mask, reg, codec->pop_time);
771 pop_wait(codec->pop_time);
772 snd_soc_update_bits(codec, reg, mask, value);
773 }
774
775 list_for_each_entry(w, pending, power_list) {
776 /* power up post event */
777 if (w->power && w->event &&
778 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
779 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
780 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000781 ret = w->event(w,
782 NULL, SND_SOC_DAPM_POST_PMU);
783 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100784 pr_err("%s: post event failed: %d\n",
785 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000786 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100787
Mark Brown81628102009-06-07 13:21:24 +0100788 /* power down post event */
789 if (!w->power && w->event &&
790 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
791 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
792 w->name);
793 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
794 if (ret < 0)
795 pr_err("%s: post event failed: %d\n",
796 w->name, ret);
797 }
Mark Brown42aa3412009-03-01 19:21:10 +0000798 }
Mark Brown42aa3412009-03-01 19:21:10 +0000799}
800
Mark Brownb22ead22009-06-07 12:51:26 +0100801/* Apply a DAPM power sequence.
802 *
803 * We walk over a pre-sorted list of widgets to apply power to. In
804 * order to minimise the number of writes to the device required
805 * multiple widgets will be updated in a single write where possible.
806 * Currently anything that requires more than a single write is not
807 * handled.
808 */
809static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
810 int event, int sort[])
811{
812 struct snd_soc_dapm_widget *w, *n;
813 LIST_HEAD(pending);
814 int cur_sort = -1;
815 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100816 int ret;
817
Mark Brownb22ead22009-06-07 12:51:26 +0100818 list_for_each_entry_safe(w, n, list, power_list) {
819 ret = 0;
820
821 /* Do we need to apply any queued changes? */
822 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
823 if (!list_empty(&pending))
824 dapm_seq_run_coalesced(codec, &pending);
825
826 INIT_LIST_HEAD(&pending);
827 cur_sort = -1;
828 cur_reg = SND_SOC_NOPM;
829 }
830
Mark Brown163cac02009-06-07 10:12:52 +0100831 switch (w->id) {
832 case snd_soc_dapm_pre:
833 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100834 list_for_each_entry_safe_continue(w, n, list,
835 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100836
Mark Brownb22ead22009-06-07 12:51:26 +0100837 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100838 ret = w->event(w,
839 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100840 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100841 ret = w->event(w,
842 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100843 break;
844
845 case snd_soc_dapm_post:
846 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100847 list_for_each_entry_safe_continue(w, n, list,
848 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100849
Mark Brownb22ead22009-06-07 12:51:26 +0100850 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100851 ret = w->event(w,
852 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100853 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100854 ret = w->event(w,
855 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100856 break;
857
858 case snd_soc_dapm_input:
859 case snd_soc_dapm_output:
860 case snd_soc_dapm_hp:
861 case snd_soc_dapm_mic:
862 case snd_soc_dapm_line:
863 case snd_soc_dapm_spk:
864 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100865 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100866 break;
867
868 default:
Mark Brown81628102009-06-07 13:21:24 +0100869 /* Queue it up for application */
870 cur_sort = sort[w->id];
871 cur_reg = w->reg;
872 list_move(&w->power_list, &pending);
873 break;
Mark Brown163cac02009-06-07 10:12:52 +0100874 }
Mark Brownb22ead22009-06-07 12:51:26 +0100875
876 if (ret < 0)
877 pr_err("Failed to apply widget power: %d\n",
878 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100879 }
Mark Brownb22ead22009-06-07 12:51:26 +0100880
881 if (!list_empty(&pending))
882 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100883}
884
Mark Brown42aa3412009-03-01 19:21:10 +0000885/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200886 * Scan each dapm widget for complete audio path.
887 * A complete path is a route that has valid endpoints i.e.:-
888 *
889 * o DAC to output pin.
890 * o Input Pin to ADC.
891 * o Input pin to Output pin (bypass, sidetone)
892 * o DAC to ADC (loopback).
893 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100894static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895{
Mark Brown452c5ea2009-05-17 21:41:23 +0100896 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100898 LIST_HEAD(up_list);
899 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100900 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100901 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100902 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200903
Mark Brown6d3ddc82009-05-16 17:47:29 +0100904 /* Check which widgets we need to power and store them in
905 * lists indicating if they should be powered up or down.
906 */
907 list_for_each_entry(w, &codec->dapm_widgets, list) {
908 switch (w->id) {
909 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100910 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100911 break;
912 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100913 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100914 break;
915
916 default:
917 if (!w->power_check)
918 continue;
919
Mark Brown99497882010-05-07 20:24:05 +0100920 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +0000921 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +0100922 else
923 power = 1;
924 if (power)
925 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100926
Mark Brown6d3ddc82009-05-16 17:47:29 +0100927 if (w->power == power)
928 continue;
929
930 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100931 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100932 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100933 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100934
935 w->power = power;
936 break;
937 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200938 }
939
Mark Brownb14b76a2009-08-17 11:55:38 +0100940 /* If there are no DAPM widgets then try to figure out power from the
941 * event type.
942 */
943 if (list_empty(&codec->dapm_widgets)) {
944 switch (event) {
945 case SND_SOC_DAPM_STREAM_START:
946 case SND_SOC_DAPM_STREAM_RESUME:
947 sys_power = 1;
948 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000949 case SND_SOC_DAPM_STREAM_SUSPEND:
950 sys_power = 0;
951 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100952 case SND_SOC_DAPM_STREAM_NOP:
Mark Browna96ca332010-01-19 22:49:43 +0000953 switch (codec->bias_level) {
954 case SND_SOC_BIAS_STANDBY:
955 case SND_SOC_BIAS_OFF:
956 sys_power = 0;
957 break;
958 default:
959 sys_power = 1;
960 break;
961 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000962 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100963 default:
964 break;
965 }
966 }
967
Mark Browna96ca332010-01-19 22:49:43 +0000968 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
969 ret = snd_soc_dapm_set_bias_level(socdev,
970 SND_SOC_BIAS_STANDBY);
971 if (ret != 0)
972 pr_err("Failed to turn on bias: %d\n", ret);
973 }
974
Mark Brown452c5ea2009-05-17 21:41:23 +0100975 /* If we're changing to all on or all off then prepare */
976 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
977 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
978 ret = snd_soc_dapm_set_bias_level(socdev,
979 SND_SOC_BIAS_PREPARE);
980 if (ret != 0)
981 pr_err("Failed to prepare bias: %d\n", ret);
982 }
983
Mark Brown6d3ddc82009-05-16 17:47:29 +0100984 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100985 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100986
987 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100988 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989
Mark Brown452c5ea2009-05-17 21:41:23 +0100990 /* If we just powered the last thing off drop to standby bias */
991 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
992 ret = snd_soc_dapm_set_bias_level(socdev,
993 SND_SOC_BIAS_STANDBY);
994 if (ret != 0)
995 pr_err("Failed to apply standby bias: %d\n", ret);
996 }
997
Mark Browna96ca332010-01-19 22:49:43 +0000998 /* If we're in standby and can support bias off then do that */
999 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
1000 codec->idle_bias_off) {
1001 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1002 if (ret != 0)
1003 pr_err("Failed to turn off bias: %d\n", ret);
1004 }
1005
Mark Brown452c5ea2009-05-17 21:41:23 +01001006 /* If we just powered up then move to active bias */
1007 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1008 ret = snd_soc_dapm_set_bias_level(socdev,
1009 SND_SOC_BIAS_ON);
1010 if (ret != 0)
1011 pr_err("Failed to apply active bias: %d\n", ret);
1012 }
1013
Mark Browncb507e72009-07-08 18:54:57 +01001014 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1015 codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +00001016 pop_wait(codec->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001017
Mark Brown42aa3412009-03-01 19:21:10 +00001018 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001019}
1020
Mark Brown79fb9382009-08-21 16:38:13 +01001021#ifdef CONFIG_DEBUG_FS
1022static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1023{
1024 file->private_data = inode->i_private;
1025 return 0;
1026}
1027
1028static ssize_t dapm_widget_power_read_file(struct file *file,
1029 char __user *user_buf,
1030 size_t count, loff_t *ppos)
1031{
1032 struct snd_soc_dapm_widget *w = file->private_data;
1033 char *buf;
1034 int in, out;
1035 ssize_t ret;
1036 struct snd_soc_dapm_path *p = NULL;
1037
1038 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1039 if (!buf)
1040 return -ENOMEM;
1041
1042 in = is_connected_input_ep(w);
1043 dapm_clear_walk(w->codec);
1044 out = is_connected_output_ep(w);
1045 dapm_clear_walk(w->codec);
1046
Mark Brownd033c362009-12-04 15:25:56 +00001047 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001048 w->name, w->power ? "On" : "Off", in, out);
1049
Mark Brownd033c362009-12-04 15:25:56 +00001050 if (w->reg >= 0)
1051 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1052 " - R%d(0x%x) bit %d",
1053 w->reg, w->reg, w->shift);
1054
1055 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1056
Mark Brown3eef08b2009-09-14 16:49:00 +01001057 if (w->sname)
1058 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1059 w->sname,
1060 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001061
1062 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001063 if (p->connected && !p->connected(w, p->sink))
1064 continue;
1065
Mark Brown79fb9382009-08-21 16:38:13 +01001066 if (p->connect)
1067 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1068 " in %s %s\n",
1069 p->name ? p->name : "static",
1070 p->source->name);
1071 }
1072 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001073 if (p->connected && !p->connected(w, p->sink))
1074 continue;
1075
Mark Brown79fb9382009-08-21 16:38:13 +01001076 if (p->connect)
1077 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1078 " out %s %s\n",
1079 p->name ? p->name : "static",
1080 p->sink->name);
1081 }
1082
1083 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1084
1085 kfree(buf);
1086 return ret;
1087}
1088
1089static const struct file_operations dapm_widget_power_fops = {
1090 .open = dapm_widget_power_open_file,
1091 .read = dapm_widget_power_read_file,
1092};
1093
1094void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1095{
1096 struct snd_soc_dapm_widget *w;
1097 struct dentry *d;
1098
1099 if (!codec->debugfs_dapm)
1100 return;
1101
1102 list_for_each_entry(w, &codec->dapm_widgets, list) {
1103 if (!w->name)
1104 continue;
1105
1106 d = debugfs_create_file(w->name, 0444,
1107 codec->debugfs_dapm, w,
1108 &dapm_widget_power_fops);
1109 if (!d)
1110 printk(KERN_WARNING
1111 "ASoC: Failed to create %s debugfs file\n",
1112 w->name);
1113 }
1114}
1115#else
1116void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1117{
1118}
1119#endif
1120
Richard Purdie2b97eab2006-10-06 18:32:18 +02001121/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001122static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001123 struct snd_kcontrol *kcontrol, int change,
1124 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001125{
1126 struct snd_soc_dapm_path *path;
1127 int found = 0;
1128
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001129 if (widget->id != snd_soc_dapm_mux &&
1130 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 return -ENODEV;
1132
Mark Brown3a655772009-10-05 17:23:30 +01001133 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001134 return 0;
1135
1136 /* find dapm widget path assoc with kcontrol */
1137 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1138 if (path->kcontrol != kcontrol)
1139 continue;
1140
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001141 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001142 continue;
1143
1144 found = 1;
1145 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001146 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147 path->connect = 1; /* new connection */
1148 else
1149 path->connect = 0; /* old connection must be powered down */
1150 }
1151
Mark Brownb91b8fa2010-01-20 18:18:35 +00001152 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001153 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1154
1155 return 0;
1156}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157
Milan plzik1b075e32008-01-10 14:39:46 +01001158/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001159static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001160 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001161{
1162 struct snd_soc_dapm_path *path;
1163 int found = 0;
1164
Milan plzik1b075e32008-01-10 14:39:46 +01001165 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001166 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001167 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001168 return -ENODEV;
1169
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170 /* find dapm widget path assoc with kcontrol */
1171 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1172 if (path->kcontrol != kcontrol)
1173 continue;
1174
1175 /* found, now check type */
1176 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001177 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001178 break;
1179 }
1180
Mark Brownb91b8fa2010-01-20 18:18:35 +00001181 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1183
1184 return 0;
1185}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186
1187/* show dapm widget status in sys fs */
1188static ssize_t dapm_widget_show(struct device *dev,
1189 struct device_attribute *attr, char *buf)
1190{
1191 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001192 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001193 struct snd_soc_dapm_widget *w;
1194 int count = 0;
1195 char *state = "not set";
1196
1197 list_for_each_entry(w, &codec->dapm_widgets, list) {
1198
1199 /* only display widgets that burnm power */
1200 switch (w->id) {
1201 case snd_soc_dapm_hp:
1202 case snd_soc_dapm_mic:
1203 case snd_soc_dapm_spk:
1204 case snd_soc_dapm_line:
1205 case snd_soc_dapm_micbias:
1206 case snd_soc_dapm_dac:
1207 case snd_soc_dapm_adc:
1208 case snd_soc_dapm_pga:
1209 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001210 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001211 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001212 if (w->name)
1213 count += sprintf(buf + count, "%s: %s\n",
1214 w->name, w->power ? "On":"Off");
1215 break;
1216 default:
1217 break;
1218 }
1219 }
1220
Mark Brown0be98982008-05-19 12:31:28 +02001221 switch (codec->bias_level) {
1222 case SND_SOC_BIAS_ON:
1223 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001224 break;
Mark Brown0be98982008-05-19 12:31:28 +02001225 case SND_SOC_BIAS_PREPARE:
1226 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001227 break;
Mark Brown0be98982008-05-19 12:31:28 +02001228 case SND_SOC_BIAS_STANDBY:
1229 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001230 break;
Mark Brown0be98982008-05-19 12:31:28 +02001231 case SND_SOC_BIAS_OFF:
1232 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001233 break;
1234 }
1235 count += sprintf(buf + count, "PM State: %s\n", state);
1236
1237 return count;
1238}
1239
1240static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1241
1242int snd_soc_dapm_sys_add(struct device *dev)
1243{
Troy Kisky12ef1932008-10-13 17:42:14 -07001244 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001245}
1246
1247static void snd_soc_dapm_sys_remove(struct device *dev)
1248{
Mark Brownaef90842009-05-16 17:53:16 +01001249 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250}
1251
1252/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001253static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001254{
1255 struct snd_soc_dapm_widget *w, *next_w;
1256 struct snd_soc_dapm_path *p, *next_p;
1257
1258 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1259 list_del(&w->list);
1260 kfree(w);
1261 }
1262
1263 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1264 list_del(&p->list);
1265 kfree(p->long_name);
1266 kfree(p);
1267 }
1268}
1269
Liam Girdwooda5302182008-07-07 13:35:17 +01001270static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001271 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001272{
1273 struct snd_soc_dapm_widget *w;
1274
1275 list_for_each_entry(w, &codec->dapm_widgets, list) {
1276 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001277 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001278 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001279 /* Allow disabling of forced pins */
1280 if (status == 0)
1281 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001282 return 0;
1283 }
1284 }
1285
Mark Brownc1286b82008-07-07 19:26:03 +01001286 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001287 return -EINVAL;
1288}
1289
Richard Purdie2b97eab2006-10-06 18:32:18 +02001290/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001291 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001292 * @codec: audio codec
1293 *
1294 * Walks all dapm audio paths and powers widgets according to their
1295 * stream or path usage.
1296 *
1297 * Returns 0 for success.
1298 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001299int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001300{
Mark Brownb91b8fa2010-01-20 18:18:35 +00001301 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001302}
Liam Girdwooda5302182008-07-07 13:35:17 +01001303EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001304
Mark Brown105f1c22008-05-13 14:52:19 +02001305static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001306 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001307{
1308 struct snd_soc_dapm_path *path;
1309 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001310 const char *sink = route->sink;
1311 const char *control = route->control;
1312 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313 int ret = 0;
1314
1315 /* find src and dest widgets */
1316 list_for_each_entry(w, &codec->dapm_widgets, list) {
1317
1318 if (!wsink && !(strcmp(w->name, sink))) {
1319 wsink = w;
1320 continue;
1321 }
1322 if (!wsource && !(strcmp(w->name, source))) {
1323 wsource = w;
1324 }
1325 }
1326
1327 if (wsource == NULL || wsink == NULL)
1328 return -ENODEV;
1329
1330 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1331 if (!path)
1332 return -ENOMEM;
1333
1334 path->source = wsource;
1335 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001336 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001337 INIT_LIST_HEAD(&path->list);
1338 INIT_LIST_HEAD(&path->list_source);
1339 INIT_LIST_HEAD(&path->list_sink);
1340
1341 /* check for external widgets */
1342 if (wsink->id == snd_soc_dapm_input) {
1343 if (wsource->id == snd_soc_dapm_micbias ||
1344 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001345 wsource->id == snd_soc_dapm_line ||
1346 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347 wsink->ext = 1;
1348 }
1349 if (wsource->id == snd_soc_dapm_output) {
1350 if (wsink->id == snd_soc_dapm_spk ||
1351 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001352 wsink->id == snd_soc_dapm_line ||
1353 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001354 wsource->ext = 1;
1355 }
1356
1357 /* connect static paths */
1358 if (control == NULL) {
1359 list_add(&path->list, &codec->dapm_paths);
1360 list_add(&path->list_sink, &wsink->sources);
1361 list_add(&path->list_source, &wsource->sinks);
1362 path->connect = 1;
1363 return 0;
1364 }
1365
1366 /* connect dynamic paths */
1367 switch(wsink->id) {
1368 case snd_soc_dapm_adc:
1369 case snd_soc_dapm_dac:
1370 case snd_soc_dapm_pga:
1371 case snd_soc_dapm_input:
1372 case snd_soc_dapm_output:
1373 case snd_soc_dapm_micbias:
1374 case snd_soc_dapm_vmid:
1375 case snd_soc_dapm_pre:
1376 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001377 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001378 case snd_soc_dapm_aif_in:
1379 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380 list_add(&path->list, &codec->dapm_paths);
1381 list_add(&path->list_sink, &wsink->sources);
1382 list_add(&path->list_source, &wsource->sinks);
1383 path->connect = 1;
1384 return 0;
1385 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001386 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001387 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1388 &wsink->kcontrols[0]);
1389 if (ret != 0)
1390 goto err;
1391 break;
1392 case snd_soc_dapm_switch:
1393 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001394 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001395 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1396 if (ret != 0)
1397 goto err;
1398 break;
1399 case snd_soc_dapm_hp:
1400 case snd_soc_dapm_mic:
1401 case snd_soc_dapm_line:
1402 case snd_soc_dapm_spk:
1403 list_add(&path->list, &codec->dapm_paths);
1404 list_add(&path->list_sink, &wsink->sources);
1405 list_add(&path->list_source, &wsource->sinks);
1406 path->connect = 0;
1407 return 0;
1408 }
1409 return 0;
1410
1411err:
1412 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1413 control, sink);
1414 kfree(path);
1415 return ret;
1416}
Mark Brown105f1c22008-05-13 14:52:19 +02001417
1418/**
Mark Brown105f1c22008-05-13 14:52:19 +02001419 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1420 * @codec: codec
1421 * @route: audio routes
1422 * @num: number of routes
1423 *
1424 * Connects 2 dapm widgets together via a named audio path. The sink is
1425 * the widget receiving the audio signal, whilst the source is the sender
1426 * of the audio signal.
1427 *
1428 * Returns 0 for success else error. On error all resources can be freed
1429 * with a call to snd_soc_card_free().
1430 */
1431int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1432 const struct snd_soc_dapm_route *route, int num)
1433{
1434 int i, ret;
1435
1436 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001437 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001438 if (ret < 0) {
1439 printk(KERN_ERR "Failed to add route %s->%s\n",
1440 route->source,
1441 route->sink);
1442 return ret;
1443 }
1444 route++;
1445 }
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1450
1451/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001452 * snd_soc_dapm_new_widgets - add new dapm widgets
1453 * @codec: audio codec
1454 *
1455 * Checks the codec for any new dapm widgets and creates them if found.
1456 *
1457 * Returns 0 for success.
1458 */
1459int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1460{
1461 struct snd_soc_dapm_widget *w;
1462
Richard Purdie2b97eab2006-10-06 18:32:18 +02001463 list_for_each_entry(w, &codec->dapm_widgets, list)
1464 {
1465 if (w->new)
1466 continue;
1467
1468 switch(w->id) {
1469 case snd_soc_dapm_switch:
1470 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001471 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001472 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473 dapm_new_mixer(codec, w);
1474 break;
1475 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001476 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001477 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001478 dapm_new_mux(codec, w);
1479 break;
1480 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001481 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001482 w->power_check = dapm_adc_check_power;
1483 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001485 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001486 w->power_check = dapm_dac_check_power;
1487 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001488 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001489 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001490 dapm_new_pga(codec, w);
1491 break;
1492 case snd_soc_dapm_input:
1493 case snd_soc_dapm_output:
1494 case snd_soc_dapm_micbias:
1495 case snd_soc_dapm_spk:
1496 case snd_soc_dapm_hp:
1497 case snd_soc_dapm_mic:
1498 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001499 w->power_check = dapm_generic_check_power;
1500 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001501 case snd_soc_dapm_supply:
1502 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001503 case snd_soc_dapm_vmid:
1504 case snd_soc_dapm_pre:
1505 case snd_soc_dapm_post:
1506 break;
1507 }
1508 w->new = 1;
1509 }
1510
1511 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001512 return 0;
1513}
1514EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1515
1516/**
1517 * snd_soc_dapm_get_volsw - dapm mixer get callback
1518 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001519 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 *
1521 * Callback to get the value of a dapm mixer control.
1522 *
1523 * Returns 0 for success.
1524 */
1525int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1527{
1528 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001529 struct soc_mixer_control *mc =
1530 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001531 unsigned int reg = mc->reg;
1532 unsigned int shift = mc->shift;
1533 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001534 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001535 unsigned int invert = mc->invert;
1536 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538 ucontrol->value.integer.value[0] =
1539 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1540 if (shift != rshift)
1541 ucontrol->value.integer.value[1] =
1542 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1543 if (invert) {
1544 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001545 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546 if (shift != rshift)
1547 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001548 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001549 }
1550
1551 return 0;
1552}
1553EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1554
1555/**
1556 * snd_soc_dapm_put_volsw - dapm mixer set callback
1557 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001558 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001559 *
1560 * Callback to set the value of a dapm mixer control.
1561 *
1562 * Returns 0 for success.
1563 */
1564int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1565 struct snd_ctl_elem_value *ucontrol)
1566{
1567 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001568 struct soc_mixer_control *mc =
1569 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001570 unsigned int reg = mc->reg;
1571 unsigned int shift = mc->shift;
1572 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001573 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001574 unsigned int mask = (1 << fls(max)) - 1;
1575 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001576 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001577 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578 int ret;
1579
1580 val = (ucontrol->value.integer.value[0] & mask);
1581
1582 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001583 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001584 val_mask = mask << shift;
1585 val = val << shift;
1586 if (shift != rshift) {
1587 val2 = (ucontrol->value.integer.value[1] & mask);
1588 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001589 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001590 val_mask |= mask << rshift;
1591 val |= val2 << rshift;
1592 }
1593
1594 mutex_lock(&widget->codec->mutex);
1595 widget->value = val;
1596
Mark Brown283375c2009-12-07 18:09:03 +00001597 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1598 if (val)
1599 /* new connection */
1600 connect = invert ? 0:1;
1601 else
1602 /* old connection must be powered down */
1603 connect = invert ? 1:0;
1604
1605 dapm_mixer_update_power(widget, kcontrol, connect);
1606 }
1607
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608 if (widget->event) {
1609 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001610 ret = widget->event(widget, kcontrol,
1611 SND_SOC_DAPM_PRE_REG);
1612 if (ret < 0) {
1613 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001614 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001615 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001616 }
1617 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1618 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001619 ret = widget->event(widget, kcontrol,
1620 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001621 } else
1622 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1623
1624out:
1625 mutex_unlock(&widget->codec->mutex);
1626 return ret;
1627}
1628EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1629
1630/**
1631 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1632 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001633 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634 *
1635 * Callback to get the value of a dapm enumerated double mixer control.
1636 *
1637 * Returns 0 for success.
1638 */
1639int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1641{
1642 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1643 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001644 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001645
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001646 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001647 ;
1648 val = snd_soc_read(widget->codec, e->reg);
1649 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1650 if (e->shift_l != e->shift_r)
1651 ucontrol->value.enumerated.item[1] =
1652 (val >> e->shift_r) & (bitmask - 1);
1653
1654 return 0;
1655}
1656EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1657
1658/**
1659 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1660 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001661 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 *
1663 * Callback to set the value of a dapm enumerated double mixer control.
1664 *
1665 * Returns 0 for success.
1666 */
1667int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1668 struct snd_ctl_elem_value *ucontrol)
1669{
1670 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1671 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001672 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001673 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 int ret = 0;
1675
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001676 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001678 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001679 return -EINVAL;
1680 mux = ucontrol->value.enumerated.item[0];
1681 val = mux << e->shift_l;
1682 mask = (bitmask - 1) << e->shift_l;
1683 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001684 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001685 return -EINVAL;
1686 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1687 mask |= (bitmask - 1) << e->shift_r;
1688 }
1689
1690 mutex_lock(&widget->codec->mutex);
1691 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001692 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1693 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001694
1695 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1696 ret = widget->event(widget,
1697 kcontrol, SND_SOC_DAPM_PRE_REG);
1698 if (ret < 0)
1699 goto out;
1700 }
1701
1702 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1703
1704 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1705 ret = widget->event(widget,
1706 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707
1708out:
1709 mutex_unlock(&widget->codec->mutex);
1710 return ret;
1711}
1712EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1713
1714/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001715 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1716 * @kcontrol: mixer control
1717 * @ucontrol: control element information
1718 *
1719 * Returns 0 for success.
1720 */
1721int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol)
1723{
1724 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1725
1726 ucontrol->value.enumerated.item[0] = widget->value;
1727
1728 return 0;
1729}
1730EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1731
1732/**
1733 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1734 * @kcontrol: mixer control
1735 * @ucontrol: control element information
1736 *
1737 * Returns 0 for success.
1738 */
1739int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1740 struct snd_ctl_elem_value *ucontrol)
1741{
1742 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1743 struct soc_enum *e =
1744 (struct soc_enum *)kcontrol->private_value;
1745 int change;
1746 int ret = 0;
1747
1748 if (ucontrol->value.enumerated.item[0] >= e->max)
1749 return -EINVAL;
1750
1751 mutex_lock(&widget->codec->mutex);
1752
1753 change = widget->value != ucontrol->value.enumerated.item[0];
1754 widget->value = ucontrol->value.enumerated.item[0];
1755 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1756
1757 mutex_unlock(&widget->codec->mutex);
1758 return ret;
1759}
1760EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1761
1762/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001763 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1764 * callback
1765 * @kcontrol: mixer control
1766 * @ucontrol: control element information
1767 *
1768 * Callback to get the value of a dapm semi enumerated double mixer control.
1769 *
1770 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1771 * used for handling bitfield coded enumeration for example.
1772 *
1773 * Returns 0 for success.
1774 */
1775int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
1777{
1778 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001779 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001780 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001781
1782 reg_val = snd_soc_read(widget->codec, e->reg);
1783 val = (reg_val >> e->shift_l) & e->mask;
1784 for (mux = 0; mux < e->max; mux++) {
1785 if (val == e->values[mux])
1786 break;
1787 }
1788 ucontrol->value.enumerated.item[0] = mux;
1789 if (e->shift_l != e->shift_r) {
1790 val = (reg_val >> e->shift_r) & e->mask;
1791 for (mux = 0; mux < e->max; mux++) {
1792 if (val == e->values[mux])
1793 break;
1794 }
1795 ucontrol->value.enumerated.item[1] = mux;
1796 }
1797
1798 return 0;
1799}
1800EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1801
1802/**
1803 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1804 * callback
1805 * @kcontrol: mixer control
1806 * @ucontrol: control element information
1807 *
1808 * Callback to set the value of a dapm semi enumerated double mixer control.
1809 *
1810 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1811 * used for handling bitfield coded enumeration for example.
1812 *
1813 * Returns 0 for success.
1814 */
1815int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
1817{
1818 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001819 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001820 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001821 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001822 int ret = 0;
1823
1824 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1825 return -EINVAL;
1826 mux = ucontrol->value.enumerated.item[0];
1827 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1828 mask = e->mask << e->shift_l;
1829 if (e->shift_l != e->shift_r) {
1830 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1831 return -EINVAL;
1832 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1833 mask |= e->mask << e->shift_r;
1834 }
1835
1836 mutex_lock(&widget->codec->mutex);
1837 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001838 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1839 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001840
1841 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1842 ret = widget->event(widget,
1843 kcontrol, SND_SOC_DAPM_PRE_REG);
1844 if (ret < 0)
1845 goto out;
1846 }
1847
1848 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1849
1850 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1851 ret = widget->event(widget,
1852 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001853
1854out:
1855 mutex_unlock(&widget->codec->mutex);
1856 return ret;
1857}
1858EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1859
1860/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001861 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1862 *
1863 * @kcontrol: mixer control
1864 * @uinfo: control element information
1865 *
1866 * Callback to provide information about a pin switch control.
1867 */
1868int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_info *uinfo)
1870{
1871 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1872 uinfo->count = 1;
1873 uinfo->value.integer.min = 0;
1874 uinfo->value.integer.max = 1;
1875
1876 return 0;
1877}
1878EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1879
1880/**
1881 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1882 *
1883 * @kcontrol: mixer control
1884 * @ucontrol: Value
1885 */
1886int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1887 struct snd_ctl_elem_value *ucontrol)
1888{
1889 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1890 const char *pin = (const char *)kcontrol->private_value;
1891
1892 mutex_lock(&codec->mutex);
1893
1894 ucontrol->value.integer.value[0] =
1895 snd_soc_dapm_get_pin_status(codec, pin);
1896
1897 mutex_unlock(&codec->mutex);
1898
1899 return 0;
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1902
1903/**
1904 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1905 *
1906 * @kcontrol: mixer control
1907 * @ucontrol: Value
1908 */
1909int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol)
1911{
1912 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1913 const char *pin = (const char *)kcontrol->private_value;
1914
1915 mutex_lock(&codec->mutex);
1916
1917 if (ucontrol->value.integer.value[0])
1918 snd_soc_dapm_enable_pin(codec, pin);
1919 else
1920 snd_soc_dapm_disable_pin(codec, pin);
1921
1922 snd_soc_dapm_sync(codec);
1923
1924 mutex_unlock(&codec->mutex);
1925
1926 return 0;
1927}
1928EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1929
1930/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001931 * snd_soc_dapm_new_control - create new dapm control
1932 * @codec: audio codec
1933 * @widget: widget template
1934 *
1935 * Creates a new dapm control based upon the template.
1936 *
1937 * Returns 0 for success else error.
1938 */
1939int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1940 const struct snd_soc_dapm_widget *widget)
1941{
1942 struct snd_soc_dapm_widget *w;
1943
1944 if ((w = dapm_cnew_widget(widget)) == NULL)
1945 return -ENOMEM;
1946
1947 w->codec = codec;
1948 INIT_LIST_HEAD(&w->sources);
1949 INIT_LIST_HEAD(&w->sinks);
1950 INIT_LIST_HEAD(&w->list);
1951 list_add(&w->list, &codec->dapm_widgets);
1952
1953 /* machine layer set ups unconnected pins and insertions */
1954 w->connected = 1;
1955 return 0;
1956}
1957EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1958
1959/**
Mark Brown4ba13272008-05-13 14:51:19 +02001960 * snd_soc_dapm_new_controls - create new dapm controls
1961 * @codec: audio codec
1962 * @widget: widget array
1963 * @num: number of widgets
1964 *
1965 * Creates new DAPM controls based upon the templates.
1966 *
1967 * Returns 0 for success else error.
1968 */
1969int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1970 const struct snd_soc_dapm_widget *widget,
1971 int num)
1972{
1973 int i, ret;
1974
1975 for (i = 0; i < num; i++) {
1976 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001977 if (ret < 0) {
1978 printk(KERN_ERR
1979 "ASoC: Failed to create DAPM control %s: %d\n",
1980 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001981 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001982 }
Mark Brown4ba13272008-05-13 14:51:19 +02001983 widget++;
1984 }
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1988
1989
1990/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1992 * @codec: audio codec
1993 * @stream: stream name
1994 * @event: stream event
1995 *
1996 * Sends a stream event to the dapm core. The core then makes any
1997 * necessary widget power changes.
1998 *
1999 * Returns 0 for success else error.
2000 */
2001int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2002 char *stream, int event)
2003{
2004 struct snd_soc_dapm_widget *w;
2005
Seth Forshee11da21a2007-02-02 17:14:19 +01002006 if (stream == NULL)
2007 return 0;
2008
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 mutex_lock(&codec->mutex);
2010 list_for_each_entry(w, &codec->dapm_widgets, list)
2011 {
2012 if (!w->sname)
2013 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002014 pr_debug("widget %s\n %s stream %s event %d\n",
2015 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 if (strstr(w->sname, stream)) {
2017 switch(event) {
2018 case SND_SOC_DAPM_STREAM_START:
2019 w->active = 1;
2020 break;
2021 case SND_SOC_DAPM_STREAM_STOP:
2022 w->active = 0;
2023 break;
2024 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2028 break;
2029 }
2030 }
2031 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032
2033 dapm_power_widgets(codec, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002034 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 return 0;
2036}
2037EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2038
2039/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002040 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002041 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002042 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002043 *
Mark Brown74b8f952009-06-06 11:26:15 +01002044 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002045 * a valid audio route and active audio stream.
2046 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2047 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002048 */
Mark Brown16499232009-01-07 18:25:13 +00002049int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002050{
Liam Girdwooda5302182008-07-07 13:35:17 +01002051 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052}
Liam Girdwooda5302182008-07-07 13:35:17 +01002053EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054
2055/**
Mark Brownda341832010-03-15 19:23:37 +00002056 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2057 * @codec: SoC codec
2058 * @pin: pin name
2059 *
2060 * Enables input/output pin regardless of any other state. This is
2061 * intended for use with microphone bias supplies used in microphone
2062 * jack detection.
2063 *
2064 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2065 * do any widget power switching.
2066 */
2067int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2068{
2069 struct snd_soc_dapm_widget *w;
2070
2071 list_for_each_entry(w, &codec->dapm_widgets, list) {
2072 if (!strcmp(w->name, pin)) {
2073 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2074 w->connected = 1;
2075 w->force = 1;
2076 return 0;
2077 }
2078 }
2079
2080 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2081 return -EINVAL;
2082}
2083EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2084
2085/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002086 * snd_soc_dapm_disable_pin - disable pin.
2087 * @codec: SoC codec
2088 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002089 *
Mark Brown74b8f952009-06-06 11:26:15 +01002090 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002091 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2092 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002093 */
Mark Brown16499232009-01-07 18:25:13 +00002094int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002095{
2096 return snd_soc_dapm_set_pin(codec, pin, 0);
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2099
2100/**
Mark Brown5817b522008-09-24 11:23:11 +01002101 * snd_soc_dapm_nc_pin - permanently disable pin.
2102 * @codec: SoC codec
2103 * @pin: pin name
2104 *
2105 * Marks the specified pin as being not connected, disabling it along
2106 * any parent or child widgets. At present this is identical to
2107 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2108 * additional things such as disabling controls which only affect
2109 * paths through the pin.
2110 *
2111 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2112 * do any widget power switching.
2113 */
Mark Brown16499232009-01-07 18:25:13 +00002114int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002115{
2116 return snd_soc_dapm_set_pin(codec, pin, 0);
2117}
2118EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2119
2120/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002121 * snd_soc_dapm_get_pin_status - get audio pin status
2122 * @codec: audio codec
2123 * @pin: audio signal pin endpoint (or start point)
2124 *
2125 * Get audio pin status - connected or disconnected.
2126 *
2127 * Returns 1 for connected otherwise 0.
2128 */
Mark Brown16499232009-01-07 18:25:13 +00002129int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002130{
2131 struct snd_soc_dapm_widget *w;
2132
2133 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002134 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002135 return w->connected;
2136 }
2137
2138 return 0;
2139}
Liam Girdwooda5302182008-07-07 13:35:17 +01002140EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002141
2142/**
Mark Brown1547aba2010-05-07 21:11:40 +01002143 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2144 * @codec: audio codec
2145 * @pin: audio signal pin endpoint (or start point)
2146 *
2147 * Mark the given endpoint or pin as ignoring suspend. When the
2148 * system is disabled a path between two endpoints flagged as ignoring
2149 * suspend will not be disabled. The path must already be enabled via
2150 * normal means at suspend time, it will not be turned on if it was not
2151 * already enabled.
2152 */
2153int snd_soc_dapm_ignore_suspend(struct snd_soc_codec *codec, const char *pin)
2154{
2155 struct snd_soc_dapm_widget *w;
2156
2157 list_for_each_entry(w, &codec->dapm_widgets, list) {
2158 if (!strcmp(w->name, pin)) {
2159 w->ignore_suspend = 1;
2160 return 0;
2161 }
2162 }
2163
2164 pr_err("Unknown DAPM pin: %s\n", pin);
2165 return -EINVAL;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2168
2169/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170 * snd_soc_dapm_free - free dapm resources
2171 * @socdev: SoC device
2172 *
2173 * Free all dapm widgets and resources.
2174 */
2175void snd_soc_dapm_free(struct snd_soc_device *socdev)
2176{
Mark Brown6627a652009-01-23 22:55:23 +00002177 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178
2179 snd_soc_dapm_sys_remove(socdev->dev);
2180 dapm_free_widgets(codec);
2181}
2182EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2183
Mark Brown51737472009-06-22 13:16:51 +01002184/*
2185 * snd_soc_dapm_shutdown - callback for system shutdown
2186 */
2187void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2188{
2189 struct snd_soc_codec *codec = socdev->card->codec;
2190 struct snd_soc_dapm_widget *w;
2191 LIST_HEAD(down_list);
2192 int powerdown = 0;
2193
2194 list_for_each_entry(w, &codec->dapm_widgets, list) {
2195 if (w->power) {
2196 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002197 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002198 powerdown = 1;
2199 }
2200 }
2201
2202 /* If there were no widgets to power down we're already in
2203 * standby.
2204 */
2205 if (powerdown) {
2206 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2207 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2208 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2209 }
2210
2211 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2212}
2213
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002215MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2217MODULE_LICENSE("GPL");