blob: 8c8b291320a87839801effbbe7a5a09c9cf03a6c [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020041#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc-dapm.h>
45#include <sound/initval.h>
46
Richard Purdie2b97eab2006-10-06 18:32:18 +020047/* dapm power sequences - make this per codec in the future */
48static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010049 [snd_soc_dapm_pre] = 0,
50 [snd_soc_dapm_supply] = 1,
51 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010052 [snd_soc_dapm_aif_in] = 3,
53 [snd_soc_dapm_aif_out] = 3,
54 [snd_soc_dapm_mic] = 4,
55 [snd_soc_dapm_mux] = 5,
56 [snd_soc_dapm_value_mux] = 5,
57 [snd_soc_dapm_dac] = 6,
58 [snd_soc_dapm_mixer] = 7,
59 [snd_soc_dapm_mixer_named_ctl] = 7,
60 [snd_soc_dapm_pga] = 8,
61 [snd_soc_dapm_adc] = 9,
62 [snd_soc_dapm_hp] = 10,
63 [snd_soc_dapm_spk] = 10,
64 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020065};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000066
Richard Purdie2b97eab2006-10-06 18:32:18 +020067static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010068 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_adc] = 1,
70 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010071 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pga] = 4,
73 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010074 [snd_soc_dapm_mixer] = 5,
75 [snd_soc_dapm_dac] = 6,
76 [snd_soc_dapm_mic] = 7,
77 [snd_soc_dapm_micbias] = 8,
78 [snd_soc_dapm_mux] = 9,
79 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010080 [snd_soc_dapm_aif_in] = 10,
81 [snd_soc_dapm_aif_out] = 10,
82 [snd_soc_dapm_supply] = 11,
83 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020084};
85
Troy Kisky12ef1932008-10-13 17:42:14 -070086static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010087{
88 if (pop_time)
89 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
90}
91
Troy Kisky12ef1932008-10-13 17:42:14 -070092static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010093{
94 va_list args;
95
96 va_start(args, fmt);
97
98 if (pop_time) {
99 vprintk(fmt, args);
Mark Brown15e4c722008-07-02 11:51:20 +0100100 }
101
102 va_end(args);
103}
104
Richard Purdie2b97eab2006-10-06 18:32:18 +0200105/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100106static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107 const struct snd_soc_dapm_widget *_widget)
108{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100109 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200110}
111
Mark Brown452c5ea2009-05-17 21:41:23 +0100112/**
113 * snd_soc_dapm_set_bias_level - set the bias level for the system
114 * @socdev: audio device
115 * @level: level to configure
116 *
117 * Configure the bias (power) levels for the SoC audio device.
118 *
119 * Returns 0 for success else error.
120 */
121static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
122 enum snd_soc_bias_level level)
123{
124 struct snd_soc_card *card = socdev->card;
125 struct snd_soc_codec *codec = socdev->card->codec;
126 int ret = 0;
127
Mark Brownf83fba82009-05-18 15:44:43 +0100128 switch (level) {
129 case SND_SOC_BIAS_ON:
130 dev_dbg(socdev->dev, "Setting full bias\n");
131 break;
132 case SND_SOC_BIAS_PREPARE:
133 dev_dbg(socdev->dev, "Setting bias prepare\n");
134 break;
135 case SND_SOC_BIAS_STANDBY:
136 dev_dbg(socdev->dev, "Setting standby bias\n");
137 break;
138 case SND_SOC_BIAS_OFF:
139 dev_dbg(socdev->dev, "Setting bias off\n");
140 break;
141 default:
142 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
143 return -EINVAL;
144 }
145
Mark Brown452c5ea2009-05-17 21:41:23 +0100146 if (card->set_bias_level)
147 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100148 if (ret == 0) {
149 if (codec->set_bias_level)
150 ret = codec->set_bias_level(codec, level);
151 else
152 codec->bias_level = level;
153 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100154
155 return ret;
156}
157
Richard Purdie2b97eab2006-10-06 18:32:18 +0200158/* set up initial codec paths */
159static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
160 struct snd_soc_dapm_path *p, int i)
161{
162 switch (w->id) {
163 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000164 case snd_soc_dapm_mixer:
165 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100167 struct soc_mixer_control *mc = (struct soc_mixer_control *)
168 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400169 unsigned int reg = mc->reg;
170 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100171 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400172 unsigned int mask = (1 << fls(max)) - 1;
173 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200174
175 val = snd_soc_read(w->codec, reg);
176 val = (val >> shift) & mask;
177
178 if ((invert && !val) || (!invert && val))
179 p->connect = 1;
180 else
181 p->connect = 0;
182 }
183 break;
184 case snd_soc_dapm_mux: {
185 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
186 int val, item, bitmask;
187
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100188 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200189 ;
190 val = snd_soc_read(w->codec, e->reg);
191 item = (val >> e->shift_l) & (bitmask - 1);
192
193 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100194 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200195 if (!(strcmp(p->name, e->texts[i])) && item == i)
196 p->connect = 1;
197 }
198 }
199 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200200 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200201 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200202 w->kcontrols[i].private_value;
203 int val, item;
204
205 val = snd_soc_read(w->codec, e->reg);
206 val = (val >> e->shift_l) & e->mask;
207 for (item = 0; item < e->max; item++) {
208 if (val == e->values[item])
209 break;
210 }
211
212 p->connect = 0;
213 for (i = 0; i < e->max; i++) {
214 if (!(strcmp(p->name, e->texts[i])) && item == i)
215 p->connect = 1;
216 }
217 }
218 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200219 /* does not effect routing - always connected */
220 case snd_soc_dapm_pga:
221 case snd_soc_dapm_output:
222 case snd_soc_dapm_adc:
223 case snd_soc_dapm_input:
224 case snd_soc_dapm_dac:
225 case snd_soc_dapm_micbias:
226 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100227 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100228 case snd_soc_dapm_aif_in:
229 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200230 p->connect = 1;
231 break;
232 /* does effect routing - dynamically connected */
233 case snd_soc_dapm_hp:
234 case snd_soc_dapm_mic:
235 case snd_soc_dapm_spk:
236 case snd_soc_dapm_line:
237 case snd_soc_dapm_pre:
238 case snd_soc_dapm_post:
239 p->connect = 0;
240 break;
241 }
242}
243
Mark Brown74b8f952009-06-06 11:26:15 +0100244/* connect mux widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200245static int dapm_connect_mux(struct snd_soc_codec *codec,
246 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
247 struct snd_soc_dapm_path *path, const char *control_name,
248 const struct snd_kcontrol_new *kcontrol)
249{
250 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
251 int i;
252
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100253 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200254 if (!(strcmp(control_name, e->texts[i]))) {
255 list_add(&path->list, &codec->dapm_paths);
256 list_add(&path->list_sink, &dest->sources);
257 list_add(&path->list_source, &src->sinks);
258 path->name = (char*)e->texts[i];
259 dapm_set_path_status(dest, path, 0);
260 return 0;
261 }
262 }
263
264 return -ENODEV;
265}
266
Mark Brown74b8f952009-06-06 11:26:15 +0100267/* connect mixer widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200268static int dapm_connect_mixer(struct snd_soc_codec *codec,
269 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
270 struct snd_soc_dapm_path *path, const char *control_name)
271{
272 int i;
273
274 /* search for mixer kcontrol */
275 for (i = 0; i < dest->num_kcontrols; i++) {
276 if (!strcmp(control_name, dest->kcontrols[i].name)) {
277 list_add(&path->list, &codec->dapm_paths);
278 list_add(&path->list_sink, &dest->sources);
279 list_add(&path->list_source, &src->sinks);
280 path->name = dest->kcontrols[i].name;
281 dapm_set_path_status(dest, path, i);
282 return 0;
283 }
284 }
285 return -ENODEV;
286}
287
288/* update dapm codec register bits */
289static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
290{
291 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300292 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 struct snd_soc_codec *codec = widget->codec;
294
295 /* check for valid widgets */
296 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
297 widget->id == snd_soc_dapm_output ||
298 widget->id == snd_soc_dapm_hp ||
299 widget->id == snd_soc_dapm_mic ||
300 widget->id == snd_soc_dapm_line ||
301 widget->id == snd_soc_dapm_spk)
302 return 0;
303
304 power = widget->power;
305 if (widget->invert)
306 power = (power ? 0:1);
307
308 old = snd_soc_read(codec, widget->reg);
309 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
310
311 change = old != new;
312 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700313 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
314 widget->name, widget->power ? "on" : "off",
315 codec->pop_time);
Troy Kisky12ef1932008-10-13 17:42:14 -0700316 pop_wait(codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000317 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200318 }
Mark Brownc1286b82008-07-07 19:26:03 +0100319 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
320 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321 return change;
322}
323
Richard Purdie2b97eab2006-10-06 18:32:18 +0200324/* create new dapm mixer control */
325static int dapm_new_mixer(struct snd_soc_codec *codec,
326 struct snd_soc_dapm_widget *w)
327{
328 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000329 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 struct snd_soc_dapm_path *path;
331
332 /* add kcontrol */
333 for (i = 0; i < w->num_kcontrols; i++) {
334
335 /* match name */
336 list_for_each_entry(path, &w->sources, list_sink) {
337
338 /* mixer/mux paths name must match control name */
339 if (path->name != (char*)w->kcontrols[i].name)
340 continue;
341
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000342 /* add dapm control with long name.
343 * for dapm_mixer this is the concatenation of the
344 * mixer and kcontrol name.
345 * for dapm_mixer_named_ctl this is simply the
346 * kcontrol name.
347 */
348 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000349 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000350 name_len += 1 + strlen(w->name);
351
Mark Brown219b93f2008-10-28 13:02:31 +0000352 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000353
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354 if (path->long_name == NULL)
355 return -ENOMEM;
356
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000357 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000358 default:
359 snprintf(path->long_name, name_len, "%s %s",
360 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000361 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000362 case snd_soc_dapm_mixer_named_ctl:
363 snprintf(path->long_name, name_len, "%s",
364 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000365 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000366 }
367
Mark Brown219b93f2008-10-28 13:02:31 +0000368 path->long_name[name_len - 1] = '\0';
369
Richard Purdie2b97eab2006-10-06 18:32:18 +0200370 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
371 path->long_name);
372 ret = snd_ctl_add(codec->card, path->kcontrol);
373 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100374 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
375 path->long_name,
376 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200377 kfree(path->long_name);
378 path->long_name = NULL;
379 return ret;
380 }
381 }
382 }
383 return ret;
384}
385
386/* create new dapm mux control */
387static int dapm_new_mux(struct snd_soc_codec *codec,
388 struct snd_soc_dapm_widget *w)
389{
390 struct snd_soc_dapm_path *path = NULL;
391 struct snd_kcontrol *kcontrol;
392 int ret = 0;
393
394 if (!w->num_kcontrols) {
395 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
396 return -EINVAL;
397 }
398
399 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
400 ret = snd_ctl_add(codec->card, kcontrol);
401 if (ret < 0)
402 goto err;
403
404 list_for_each_entry(path, &w->sources, list_sink)
405 path->kcontrol = kcontrol;
406
407 return ret;
408
409err:
410 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
411 return ret;
412}
413
414/* create new dapm volume control */
415static int dapm_new_pga(struct snd_soc_codec *codec,
416 struct snd_soc_dapm_widget *w)
417{
Mark Browna6c65732010-03-03 17:45:21 +0000418 if (w->num_kcontrols)
419 pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420
Mark Browna6c65732010-03-03 17:45:21 +0000421 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422}
423
424/* reset 'walked' bit for each dapm path */
425static inline void dapm_clear_walk(struct snd_soc_codec *codec)
426{
427 struct snd_soc_dapm_path *p;
428
429 list_for_each_entry(p, &codec->dapm_paths, list)
430 p->walked = 0;
431}
432
Mark Brown99497882010-05-07 20:24:05 +0100433/* We implement power down on suspend by checking the power state of
434 * the ALSA card - when we are suspending the ALSA state for the card
435 * is set to D3.
436 */
437static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
438{
439 struct snd_soc_codec *codec = widget->codec;
440
441 switch (snd_power_get_state(codec->card)) {
442 case SNDRV_CTL_POWER_D3hot:
443 case SNDRV_CTL_POWER_D3cold:
444 return 0;
445 default:
446 return 1;
447 }
448}
449
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450/*
451 * Recursively check for a completed path to an active or physically connected
452 * output widget. Returns number of complete paths.
453 */
454static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
455{
456 struct snd_soc_dapm_path *path;
457 int con = 0;
458
Mark Brown246d0a12009-04-22 18:24:55 +0100459 if (widget->id == snd_soc_dapm_supply)
460 return 0;
461
Mark Brown010ff262009-08-17 17:39:22 +0100462 switch (widget->id) {
463 case snd_soc_dapm_adc:
464 case snd_soc_dapm_aif_out:
465 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100466 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100467 default:
468 break;
469 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470
471 if (widget->connected) {
472 /* connected pin ? */
473 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100474 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475
476 /* connected jack or spk ? */
477 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300478 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100479 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480 }
481
482 list_for_each_entry(path, &widget->sinks, list_source) {
483 if (path->walked)
484 continue;
485
486 if (path->sink && path->connect) {
487 path->walked = 1;
488 con += is_connected_output_ep(path->sink);
489 }
490 }
491
492 return con;
493}
494
495/*
496 * Recursively check for a completed path to an active or physically connected
497 * input widget. Returns number of complete paths.
498 */
499static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
500{
501 struct snd_soc_dapm_path *path;
502 int con = 0;
503
Mark Brown246d0a12009-04-22 18:24:55 +0100504 if (widget->id == snd_soc_dapm_supply)
505 return 0;
506
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100508 switch (widget->id) {
509 case snd_soc_dapm_dac:
510 case snd_soc_dapm_aif_in:
511 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100512 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100513 default:
514 break;
515 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516
517 if (widget->connected) {
518 /* connected pin ? */
519 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100520 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521
522 /* connected VMID/Bias for lower pops */
523 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100524 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525
526 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300527 if (widget->id == snd_soc_dapm_mic ||
528 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100529 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200530 }
531
532 list_for_each_entry(path, &widget->sources, list_sink) {
533 if (path->walked)
534 continue;
535
536 if (path->source && path->connect) {
537 path->walked = 1;
538 con += is_connected_input_ep(path->source);
539 }
540 }
541
542 return con;
543}
544
545/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300546 * Handler for generic register modifier widget.
547 */
548int dapm_reg_event(struct snd_soc_dapm_widget *w,
549 struct snd_kcontrol *kcontrol, int event)
550{
551 unsigned int val;
552
553 if (SND_SOC_DAPM_EVENT_ON(event))
554 val = w->on_val;
555 else
556 val = w->off_val;
557
558 snd_soc_update_bits(w->codec, -(w->reg + 1),
559 w->mask << w->shift, val << w->shift);
560
561 return 0;
562}
Mark Brown11589412008-07-29 11:42:23 +0100563EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300564
Mark Brown025756e2009-04-13 11:09:18 +0100565/* Standard power change method, used to apply power changes to most
566 * widgets.
567 */
568static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
569{
570 int ret;
571
572 /* call any power change event handlers */
573 if (w->event)
574 pr_debug("power %s event for %s flags %x\n",
575 w->power ? "on" : "off",
576 w->name, w->event_flags);
577
578 /* power up pre event */
579 if (w->power && w->event &&
580 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
581 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
582 if (ret < 0)
583 return ret;
584 }
585
586 /* power down pre event */
587 if (!w->power && w->event &&
588 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
589 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
590 if (ret < 0)
591 return ret;
592 }
593
Mark Brown025756e2009-04-13 11:09:18 +0100594 dapm_update_bits(w);
595
Mark Brown025756e2009-04-13 11:09:18 +0100596 /* power up post event */
597 if (w->power && w->event &&
598 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
599 ret = w->event(w,
600 NULL, SND_SOC_DAPM_POST_PMU);
601 if (ret < 0)
602 return ret;
603 }
604
605 /* power down post event */
606 if (!w->power && w->event &&
607 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
608 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
609 if (ret < 0)
610 return ret;
611 }
612
613 return 0;
614}
615
Mark Browncd0f2d42009-04-20 16:56:59 +0100616/* Generic check to see if a widget should be powered.
617 */
618static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
619{
620 int in, out;
621
622 in = is_connected_input_ep(w);
623 dapm_clear_walk(w->codec);
624 out = is_connected_output_ep(w);
625 dapm_clear_walk(w->codec);
626 return out != 0 && in != 0;
627}
628
Mark Brown6ea31b92009-04-20 17:15:41 +0100629/* Check to see if an ADC has power */
630static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
631{
632 int in;
633
634 if (w->active) {
635 in = is_connected_input_ep(w);
636 dapm_clear_walk(w->codec);
637 return in != 0;
638 } else {
639 return dapm_generic_check_power(w);
640 }
641}
642
643/* Check to see if a DAC has power */
644static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
645{
646 int out;
647
648 if (w->active) {
649 out = is_connected_output_ep(w);
650 dapm_clear_walk(w->codec);
651 return out != 0;
652 } else {
653 return dapm_generic_check_power(w);
654 }
655}
656
Mark Brown246d0a12009-04-22 18:24:55 +0100657/* Check to see if a power supply is needed */
658static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
659{
660 struct snd_soc_dapm_path *path;
661 int power = 0;
662
663 /* Check if one of our outputs is connected */
664 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100665 if (path->connected &&
666 !path->connected(path->source, path->sink))
667 continue;
668
Mark Brown246d0a12009-04-22 18:24:55 +0100669 if (path->sink && path->sink->power_check &&
670 path->sink->power_check(path->sink)) {
671 power = 1;
672 break;
673 }
674 }
675
676 dapm_clear_walk(w->codec);
677
678 return power;
679}
680
Mark Brown38357ab2009-06-06 19:03:23 +0100681static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
682 struct snd_soc_dapm_widget *b,
683 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000684{
Mark Brownd207c682009-12-07 17:13:55 +0000685 if (a->codec != b->codec)
686 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100687 if (sort[a->id] != sort[b->id])
688 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100689 if (a->reg != b->reg)
690 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000691
Mark Brown38357ab2009-06-06 19:03:23 +0100692 return 0;
693}
Mark Brown42aa3412009-03-01 19:21:10 +0000694
Mark Brown38357ab2009-06-06 19:03:23 +0100695/* Insert a widget in order into a DAPM power sequence. */
696static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
697 struct list_head *list,
698 int sort[])
699{
700 struct snd_soc_dapm_widget *w;
701
702 list_for_each_entry(w, list, power_list)
703 if (dapm_seq_compare(new_widget, w, sort) < 0) {
704 list_add_tail(&new_widget->power_list, &w->power_list);
705 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000706 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100707
Mark Brown38357ab2009-06-06 19:03:23 +0100708 list_add_tail(&new_widget->power_list, list);
709}
Mark Brown42aa3412009-03-01 19:21:10 +0000710
Mark Brownb22ead22009-06-07 12:51:26 +0100711/* Apply the coalesced changes from a DAPM sequence */
712static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
713 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100714{
715 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100716 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100717 unsigned int value = 0;
718 unsigned int mask = 0;
719 unsigned int cur_mask;
720
721 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
722 power_list)->reg;
723
724 list_for_each_entry(w, pending, power_list) {
725 cur_mask = 1 << w->shift;
726 BUG_ON(reg != w->reg);
727
728 if (w->invert)
729 power = !w->power;
730 else
731 power = w->power;
732
733 mask |= cur_mask;
734 if (power)
735 value |= cur_mask;
736
737 pop_dbg(codec->pop_time,
738 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
739 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100740
741 /* power up pre event */
742 if (w->power && w->event &&
743 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
744 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
745 w->name);
746 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
747 if (ret < 0)
748 pr_err("%s: pre event failed: %d\n",
749 w->name, ret);
750 }
751
752 /* power down pre event */
753 if (!w->power && w->event &&
754 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
755 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
756 w->name);
757 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
758 if (ret < 0)
759 pr_err("%s: pre event failed: %d\n",
760 w->name, ret);
761 }
Mark Brownb22ead22009-06-07 12:51:26 +0100762 }
763
Mark Brown81628102009-06-07 13:21:24 +0100764 if (reg >= 0) {
765 pop_dbg(codec->pop_time,
766 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
767 value, mask, reg, codec->pop_time);
768 pop_wait(codec->pop_time);
769 snd_soc_update_bits(codec, reg, mask, value);
770 }
771
772 list_for_each_entry(w, pending, power_list) {
773 /* power up post event */
774 if (w->power && w->event &&
775 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
776 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
777 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000778 ret = w->event(w,
779 NULL, SND_SOC_DAPM_POST_PMU);
780 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100781 pr_err("%s: post event failed: %d\n",
782 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000783 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100784
Mark Brown81628102009-06-07 13:21:24 +0100785 /* power down post event */
786 if (!w->power && w->event &&
787 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
788 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
789 w->name);
790 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
791 if (ret < 0)
792 pr_err("%s: post event failed: %d\n",
793 w->name, ret);
794 }
Mark Brown42aa3412009-03-01 19:21:10 +0000795 }
Mark Brown42aa3412009-03-01 19:21:10 +0000796}
797
Mark Brownb22ead22009-06-07 12:51:26 +0100798/* Apply a DAPM power sequence.
799 *
800 * We walk over a pre-sorted list of widgets to apply power to. In
801 * order to minimise the number of writes to the device required
802 * multiple widgets will be updated in a single write where possible.
803 * Currently anything that requires more than a single write is not
804 * handled.
805 */
806static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
807 int event, int sort[])
808{
809 struct snd_soc_dapm_widget *w, *n;
810 LIST_HEAD(pending);
811 int cur_sort = -1;
812 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100813 int ret;
814
Mark Brownb22ead22009-06-07 12:51:26 +0100815 list_for_each_entry_safe(w, n, list, power_list) {
816 ret = 0;
817
818 /* Do we need to apply any queued changes? */
819 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
820 if (!list_empty(&pending))
821 dapm_seq_run_coalesced(codec, &pending);
822
823 INIT_LIST_HEAD(&pending);
824 cur_sort = -1;
825 cur_reg = SND_SOC_NOPM;
826 }
827
Mark Brown163cac02009-06-07 10:12:52 +0100828 switch (w->id) {
829 case snd_soc_dapm_pre:
830 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100831 list_for_each_entry_safe_continue(w, n, list,
832 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100833
Mark Brownb22ead22009-06-07 12:51:26 +0100834 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100835 ret = w->event(w,
836 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100837 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100838 ret = w->event(w,
839 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100840 break;
841
842 case snd_soc_dapm_post:
843 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100844 list_for_each_entry_safe_continue(w, n, list,
845 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100846
Mark Brownb22ead22009-06-07 12:51:26 +0100847 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100848 ret = w->event(w,
849 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100850 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100851 ret = w->event(w,
852 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100853 break;
854
855 case snd_soc_dapm_input:
856 case snd_soc_dapm_output:
857 case snd_soc_dapm_hp:
858 case snd_soc_dapm_mic:
859 case snd_soc_dapm_line:
860 case snd_soc_dapm_spk:
861 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100862 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100863 break;
864
865 default:
Mark Brown81628102009-06-07 13:21:24 +0100866 /* Queue it up for application */
867 cur_sort = sort[w->id];
868 cur_reg = w->reg;
869 list_move(&w->power_list, &pending);
870 break;
Mark Brown163cac02009-06-07 10:12:52 +0100871 }
Mark Brownb22ead22009-06-07 12:51:26 +0100872
873 if (ret < 0)
874 pr_err("Failed to apply widget power: %d\n",
875 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100876 }
Mark Brownb22ead22009-06-07 12:51:26 +0100877
878 if (!list_empty(&pending))
879 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100880}
881
Mark Brown42aa3412009-03-01 19:21:10 +0000882/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883 * Scan each dapm widget for complete audio path.
884 * A complete path is a route that has valid endpoints i.e.:-
885 *
886 * o DAC to output pin.
887 * o Input Pin to ADC.
888 * o Input pin to Output pin (bypass, sidetone)
889 * o DAC to ADC (loopback).
890 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100891static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200892{
Mark Brown452c5ea2009-05-17 21:41:23 +0100893 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200894 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100895 LIST_HEAD(up_list);
896 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100897 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100898 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100899 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200900
Mark Brown6d3ddc82009-05-16 17:47:29 +0100901 /* Check which widgets we need to power and store them in
902 * lists indicating if they should be powered up or down.
903 */
904 list_for_each_entry(w, &codec->dapm_widgets, list) {
905 switch (w->id) {
906 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100907 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100908 break;
909 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100910 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100911 break;
912
913 default:
914 if (!w->power_check)
915 continue;
916
Mark Brown99497882010-05-07 20:24:05 +0100917 if (!w->force)
918 power = w->power_check(w);
919 else
920 power = 1;
921 if (power)
922 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100923
Mark Brown6d3ddc82009-05-16 17:47:29 +0100924 if (w->power == power)
925 continue;
926
927 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100928 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100929 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100930 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100931
932 w->power = power;
933 break;
934 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 }
936
Mark Brownb14b76a2009-08-17 11:55:38 +0100937 /* If there are no DAPM widgets then try to figure out power from the
938 * event type.
939 */
940 if (list_empty(&codec->dapm_widgets)) {
941 switch (event) {
942 case SND_SOC_DAPM_STREAM_START:
943 case SND_SOC_DAPM_STREAM_RESUME:
944 sys_power = 1;
945 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000946 case SND_SOC_DAPM_STREAM_SUSPEND:
947 sys_power = 0;
948 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100949 case SND_SOC_DAPM_STREAM_NOP:
Mark Browna96ca332010-01-19 22:49:43 +0000950 switch (codec->bias_level) {
951 case SND_SOC_BIAS_STANDBY:
952 case SND_SOC_BIAS_OFF:
953 sys_power = 0;
954 break;
955 default:
956 sys_power = 1;
957 break;
958 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000959 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100960 default:
961 break;
962 }
963 }
964
Mark Browna96ca332010-01-19 22:49:43 +0000965 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
966 ret = snd_soc_dapm_set_bias_level(socdev,
967 SND_SOC_BIAS_STANDBY);
968 if (ret != 0)
969 pr_err("Failed to turn on bias: %d\n", ret);
970 }
971
Mark Brown452c5ea2009-05-17 21:41:23 +0100972 /* If we're changing to all on or all off then prepare */
973 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
974 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
975 ret = snd_soc_dapm_set_bias_level(socdev,
976 SND_SOC_BIAS_PREPARE);
977 if (ret != 0)
978 pr_err("Failed to prepare bias: %d\n", ret);
979 }
980
Mark Brown6d3ddc82009-05-16 17:47:29 +0100981 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100982 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100983
984 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100985 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200986
Mark Brown452c5ea2009-05-17 21:41:23 +0100987 /* If we just powered the last thing off drop to standby bias */
988 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
989 ret = snd_soc_dapm_set_bias_level(socdev,
990 SND_SOC_BIAS_STANDBY);
991 if (ret != 0)
992 pr_err("Failed to apply standby bias: %d\n", ret);
993 }
994
Mark Browna96ca332010-01-19 22:49:43 +0000995 /* If we're in standby and can support bias off then do that */
996 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
997 codec->idle_bias_off) {
998 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
999 if (ret != 0)
1000 pr_err("Failed to turn off bias: %d\n", ret);
1001 }
1002
Mark Brown452c5ea2009-05-17 21:41:23 +01001003 /* If we just powered up then move to active bias */
1004 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1005 ret = snd_soc_dapm_set_bias_level(socdev,
1006 SND_SOC_BIAS_ON);
1007 if (ret != 0)
1008 pr_err("Failed to apply active bias: %d\n", ret);
1009 }
1010
Mark Browncb507e72009-07-08 18:54:57 +01001011 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1012 codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +00001013 pop_wait(codec->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001014
Mark Brown42aa3412009-03-01 19:21:10 +00001015 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001016}
1017
Mark Brown79fb9382009-08-21 16:38:13 +01001018#ifdef CONFIG_DEBUG_FS
1019static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1020{
1021 file->private_data = inode->i_private;
1022 return 0;
1023}
1024
1025static ssize_t dapm_widget_power_read_file(struct file *file,
1026 char __user *user_buf,
1027 size_t count, loff_t *ppos)
1028{
1029 struct snd_soc_dapm_widget *w = file->private_data;
1030 char *buf;
1031 int in, out;
1032 ssize_t ret;
1033 struct snd_soc_dapm_path *p = NULL;
1034
1035 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1036 if (!buf)
1037 return -ENOMEM;
1038
1039 in = is_connected_input_ep(w);
1040 dapm_clear_walk(w->codec);
1041 out = is_connected_output_ep(w);
1042 dapm_clear_walk(w->codec);
1043
Mark Brownd033c362009-12-04 15:25:56 +00001044 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001045 w->name, w->power ? "On" : "Off", in, out);
1046
Mark Brownd033c362009-12-04 15:25:56 +00001047 if (w->reg >= 0)
1048 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1049 " - R%d(0x%x) bit %d",
1050 w->reg, w->reg, w->shift);
1051
1052 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1053
Mark Brown3eef08b2009-09-14 16:49:00 +01001054 if (w->sname)
1055 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1056 w->sname,
1057 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001058
1059 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001060 if (p->connected && !p->connected(w, p->sink))
1061 continue;
1062
Mark Brown79fb9382009-08-21 16:38:13 +01001063 if (p->connect)
1064 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1065 " in %s %s\n",
1066 p->name ? p->name : "static",
1067 p->source->name);
1068 }
1069 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001070 if (p->connected && !p->connected(w, p->sink))
1071 continue;
1072
Mark Brown79fb9382009-08-21 16:38:13 +01001073 if (p->connect)
1074 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1075 " out %s %s\n",
1076 p->name ? p->name : "static",
1077 p->sink->name);
1078 }
1079
1080 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1081
1082 kfree(buf);
1083 return ret;
1084}
1085
1086static const struct file_operations dapm_widget_power_fops = {
1087 .open = dapm_widget_power_open_file,
1088 .read = dapm_widget_power_read_file,
1089};
1090
1091void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1092{
1093 struct snd_soc_dapm_widget *w;
1094 struct dentry *d;
1095
1096 if (!codec->debugfs_dapm)
1097 return;
1098
1099 list_for_each_entry(w, &codec->dapm_widgets, list) {
1100 if (!w->name)
1101 continue;
1102
1103 d = debugfs_create_file(w->name, 0444,
1104 codec->debugfs_dapm, w,
1105 &dapm_widget_power_fops);
1106 if (!d)
1107 printk(KERN_WARNING
1108 "ASoC: Failed to create %s debugfs file\n",
1109 w->name);
1110 }
1111}
1112#else
1113void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1114{
1115}
1116#endif
1117
Richard Purdie2b97eab2006-10-06 18:32:18 +02001118/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001119static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001120 struct snd_kcontrol *kcontrol, int change,
1121 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001122{
1123 struct snd_soc_dapm_path *path;
1124 int found = 0;
1125
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001126 if (widget->id != snd_soc_dapm_mux &&
1127 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001128 return -ENODEV;
1129
Mark Brown3a655772009-10-05 17:23:30 +01001130 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 return 0;
1132
1133 /* find dapm widget path assoc with kcontrol */
1134 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1135 if (path->kcontrol != kcontrol)
1136 continue;
1137
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001138 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139 continue;
1140
1141 found = 1;
1142 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001143 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001144 path->connect = 1; /* new connection */
1145 else
1146 path->connect = 0; /* old connection must be powered down */
1147 }
1148
Mark Brownb91b8fa2010-01-20 18:18:35 +00001149 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001150 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1151
1152 return 0;
1153}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154
Milan plzik1b075e32008-01-10 14:39:46 +01001155/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001156static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001157 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001158{
1159 struct snd_soc_dapm_path *path;
1160 int found = 0;
1161
Milan plzik1b075e32008-01-10 14:39:46 +01001162 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001163 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001164 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001165 return -ENODEV;
1166
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167 /* find dapm widget path assoc with kcontrol */
1168 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1169 if (path->kcontrol != kcontrol)
1170 continue;
1171
1172 /* found, now check type */
1173 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001174 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175 break;
1176 }
1177
Mark Brownb91b8fa2010-01-20 18:18:35 +00001178 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001179 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1180
1181 return 0;
1182}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183
1184/* show dapm widget status in sys fs */
1185static ssize_t dapm_widget_show(struct device *dev,
1186 struct device_attribute *attr, char *buf)
1187{
1188 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001189 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001190 struct snd_soc_dapm_widget *w;
1191 int count = 0;
1192 char *state = "not set";
1193
1194 list_for_each_entry(w, &codec->dapm_widgets, list) {
1195
1196 /* only display widgets that burnm power */
1197 switch (w->id) {
1198 case snd_soc_dapm_hp:
1199 case snd_soc_dapm_mic:
1200 case snd_soc_dapm_spk:
1201 case snd_soc_dapm_line:
1202 case snd_soc_dapm_micbias:
1203 case snd_soc_dapm_dac:
1204 case snd_soc_dapm_adc:
1205 case snd_soc_dapm_pga:
1206 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001207 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001208 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209 if (w->name)
1210 count += sprintf(buf + count, "%s: %s\n",
1211 w->name, w->power ? "On":"Off");
1212 break;
1213 default:
1214 break;
1215 }
1216 }
1217
Mark Brown0be98982008-05-19 12:31:28 +02001218 switch (codec->bias_level) {
1219 case SND_SOC_BIAS_ON:
1220 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001221 break;
Mark Brown0be98982008-05-19 12:31:28 +02001222 case SND_SOC_BIAS_PREPARE:
1223 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001224 break;
Mark Brown0be98982008-05-19 12:31:28 +02001225 case SND_SOC_BIAS_STANDBY:
1226 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001227 break;
Mark Brown0be98982008-05-19 12:31:28 +02001228 case SND_SOC_BIAS_OFF:
1229 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001230 break;
1231 }
1232 count += sprintf(buf + count, "PM State: %s\n", state);
1233
1234 return count;
1235}
1236
1237static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1238
1239int snd_soc_dapm_sys_add(struct device *dev)
1240{
Troy Kisky12ef1932008-10-13 17:42:14 -07001241 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001242}
1243
1244static void snd_soc_dapm_sys_remove(struct device *dev)
1245{
Mark Brownaef90842009-05-16 17:53:16 +01001246 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001247}
1248
1249/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001250static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001251{
1252 struct snd_soc_dapm_widget *w, *next_w;
1253 struct snd_soc_dapm_path *p, *next_p;
1254
1255 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1256 list_del(&w->list);
1257 kfree(w);
1258 }
1259
1260 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1261 list_del(&p->list);
1262 kfree(p->long_name);
1263 kfree(p);
1264 }
1265}
1266
Liam Girdwooda5302182008-07-07 13:35:17 +01001267static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001268 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001269{
1270 struct snd_soc_dapm_widget *w;
1271
1272 list_for_each_entry(w, &codec->dapm_widgets, list) {
1273 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001274 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001275 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001276 /* Allow disabling of forced pins */
1277 if (status == 0)
1278 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001279 return 0;
1280 }
1281 }
1282
Mark Brownc1286b82008-07-07 19:26:03 +01001283 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001284 return -EINVAL;
1285}
1286
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001288 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289 * @codec: audio codec
1290 *
1291 * Walks all dapm audio paths and powers widgets according to their
1292 * stream or path usage.
1293 *
1294 * Returns 0 for success.
1295 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001296int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001297{
Mark Brownb91b8fa2010-01-20 18:18:35 +00001298 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299}
Liam Girdwooda5302182008-07-07 13:35:17 +01001300EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301
Mark Brown105f1c22008-05-13 14:52:19 +02001302static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001303 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001304{
1305 struct snd_soc_dapm_path *path;
1306 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001307 const char *sink = route->sink;
1308 const char *control = route->control;
1309 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001310 int ret = 0;
1311
1312 /* find src and dest widgets */
1313 list_for_each_entry(w, &codec->dapm_widgets, list) {
1314
1315 if (!wsink && !(strcmp(w->name, sink))) {
1316 wsink = w;
1317 continue;
1318 }
1319 if (!wsource && !(strcmp(w->name, source))) {
1320 wsource = w;
1321 }
1322 }
1323
1324 if (wsource == NULL || wsink == NULL)
1325 return -ENODEV;
1326
1327 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1328 if (!path)
1329 return -ENOMEM;
1330
1331 path->source = wsource;
1332 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001333 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 INIT_LIST_HEAD(&path->list);
1335 INIT_LIST_HEAD(&path->list_source);
1336 INIT_LIST_HEAD(&path->list_sink);
1337
1338 /* check for external widgets */
1339 if (wsink->id == snd_soc_dapm_input) {
1340 if (wsource->id == snd_soc_dapm_micbias ||
1341 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001342 wsource->id == snd_soc_dapm_line ||
1343 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001344 wsink->ext = 1;
1345 }
1346 if (wsource->id == snd_soc_dapm_output) {
1347 if (wsink->id == snd_soc_dapm_spk ||
1348 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001349 wsink->id == snd_soc_dapm_line ||
1350 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001351 wsource->ext = 1;
1352 }
1353
1354 /* connect static paths */
1355 if (control == NULL) {
1356 list_add(&path->list, &codec->dapm_paths);
1357 list_add(&path->list_sink, &wsink->sources);
1358 list_add(&path->list_source, &wsource->sinks);
1359 path->connect = 1;
1360 return 0;
1361 }
1362
1363 /* connect dynamic paths */
1364 switch(wsink->id) {
1365 case snd_soc_dapm_adc:
1366 case snd_soc_dapm_dac:
1367 case snd_soc_dapm_pga:
1368 case snd_soc_dapm_input:
1369 case snd_soc_dapm_output:
1370 case snd_soc_dapm_micbias:
1371 case snd_soc_dapm_vmid:
1372 case snd_soc_dapm_pre:
1373 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001374 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001375 case snd_soc_dapm_aif_in:
1376 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377 list_add(&path->list, &codec->dapm_paths);
1378 list_add(&path->list_sink, &wsink->sources);
1379 list_add(&path->list_source, &wsource->sinks);
1380 path->connect = 1;
1381 return 0;
1382 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001383 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001384 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1385 &wsink->kcontrols[0]);
1386 if (ret != 0)
1387 goto err;
1388 break;
1389 case snd_soc_dapm_switch:
1390 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001391 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001392 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1393 if (ret != 0)
1394 goto err;
1395 break;
1396 case snd_soc_dapm_hp:
1397 case snd_soc_dapm_mic:
1398 case snd_soc_dapm_line:
1399 case snd_soc_dapm_spk:
1400 list_add(&path->list, &codec->dapm_paths);
1401 list_add(&path->list_sink, &wsink->sources);
1402 list_add(&path->list_source, &wsource->sinks);
1403 path->connect = 0;
1404 return 0;
1405 }
1406 return 0;
1407
1408err:
1409 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1410 control, sink);
1411 kfree(path);
1412 return ret;
1413}
Mark Brown105f1c22008-05-13 14:52:19 +02001414
1415/**
Mark Brown105f1c22008-05-13 14:52:19 +02001416 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1417 * @codec: codec
1418 * @route: audio routes
1419 * @num: number of routes
1420 *
1421 * Connects 2 dapm widgets together via a named audio path. The sink is
1422 * the widget receiving the audio signal, whilst the source is the sender
1423 * of the audio signal.
1424 *
1425 * Returns 0 for success else error. On error all resources can be freed
1426 * with a call to snd_soc_card_free().
1427 */
1428int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1429 const struct snd_soc_dapm_route *route, int num)
1430{
1431 int i, ret;
1432
1433 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001434 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001435 if (ret < 0) {
1436 printk(KERN_ERR "Failed to add route %s->%s\n",
1437 route->source,
1438 route->sink);
1439 return ret;
1440 }
1441 route++;
1442 }
1443
1444 return 0;
1445}
1446EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1447
1448/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001449 * snd_soc_dapm_new_widgets - add new dapm widgets
1450 * @codec: audio codec
1451 *
1452 * Checks the codec for any new dapm widgets and creates them if found.
1453 *
1454 * Returns 0 for success.
1455 */
1456int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1457{
1458 struct snd_soc_dapm_widget *w;
1459
Richard Purdie2b97eab2006-10-06 18:32:18 +02001460 list_for_each_entry(w, &codec->dapm_widgets, list)
1461 {
1462 if (w->new)
1463 continue;
1464
1465 switch(w->id) {
1466 case snd_soc_dapm_switch:
1467 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001468 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001469 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470 dapm_new_mixer(codec, w);
1471 break;
1472 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001473 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001474 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475 dapm_new_mux(codec, w);
1476 break;
1477 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001478 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001479 w->power_check = dapm_adc_check_power;
1480 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001481 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001482 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001483 w->power_check = dapm_dac_check_power;
1484 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001485 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001486 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001487 dapm_new_pga(codec, w);
1488 break;
1489 case snd_soc_dapm_input:
1490 case snd_soc_dapm_output:
1491 case snd_soc_dapm_micbias:
1492 case snd_soc_dapm_spk:
1493 case snd_soc_dapm_hp:
1494 case snd_soc_dapm_mic:
1495 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001496 w->power_check = dapm_generic_check_power;
1497 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001498 case snd_soc_dapm_supply:
1499 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001500 case snd_soc_dapm_vmid:
1501 case snd_soc_dapm_pre:
1502 case snd_soc_dapm_post:
1503 break;
1504 }
1505 w->new = 1;
1506 }
1507
1508 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509 return 0;
1510}
1511EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1512
1513/**
1514 * snd_soc_dapm_get_volsw - dapm mixer get callback
1515 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001516 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 *
1518 * Callback to get the value of a dapm mixer control.
1519 *
1520 * Returns 0 for success.
1521 */
1522int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1523 struct snd_ctl_elem_value *ucontrol)
1524{
1525 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001526 struct soc_mixer_control *mc =
1527 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001528 unsigned int reg = mc->reg;
1529 unsigned int shift = mc->shift;
1530 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001531 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001532 unsigned int invert = mc->invert;
1533 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534
Richard Purdie2b97eab2006-10-06 18:32:18 +02001535 ucontrol->value.integer.value[0] =
1536 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1537 if (shift != rshift)
1538 ucontrol->value.integer.value[1] =
1539 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1540 if (invert) {
1541 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001542 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543 if (shift != rshift)
1544 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001545 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546 }
1547
1548 return 0;
1549}
1550EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1551
1552/**
1553 * snd_soc_dapm_put_volsw - dapm mixer set callback
1554 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001555 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001556 *
1557 * Callback to set the value of a dapm mixer control.
1558 *
1559 * Returns 0 for success.
1560 */
1561int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_value *ucontrol)
1563{
1564 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001565 struct soc_mixer_control *mc =
1566 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001567 unsigned int reg = mc->reg;
1568 unsigned int shift = mc->shift;
1569 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001570 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001571 unsigned int mask = (1 << fls(max)) - 1;
1572 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001573 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001574 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 int ret;
1576
1577 val = (ucontrol->value.integer.value[0] & mask);
1578
1579 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001580 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581 val_mask = mask << shift;
1582 val = val << shift;
1583 if (shift != rshift) {
1584 val2 = (ucontrol->value.integer.value[1] & mask);
1585 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001586 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001587 val_mask |= mask << rshift;
1588 val |= val2 << rshift;
1589 }
1590
1591 mutex_lock(&widget->codec->mutex);
1592 widget->value = val;
1593
Mark Brown283375c2009-12-07 18:09:03 +00001594 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1595 if (val)
1596 /* new connection */
1597 connect = invert ? 0:1;
1598 else
1599 /* old connection must be powered down */
1600 connect = invert ? 1:0;
1601
1602 dapm_mixer_update_power(widget, kcontrol, connect);
1603 }
1604
Richard Purdie2b97eab2006-10-06 18:32:18 +02001605 if (widget->event) {
1606 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001607 ret = widget->event(widget, kcontrol,
1608 SND_SOC_DAPM_PRE_REG);
1609 if (ret < 0) {
1610 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001611 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001612 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 }
1614 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1615 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001616 ret = widget->event(widget, kcontrol,
1617 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 } else
1619 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1620
1621out:
1622 mutex_unlock(&widget->codec->mutex);
1623 return ret;
1624}
1625EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1626
1627/**
1628 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1629 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001630 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 *
1632 * Callback to get the value of a dapm enumerated double mixer control.
1633 *
1634 * Returns 0 for success.
1635 */
1636int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
1638{
1639 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1640 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001641 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001642
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001643 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 ;
1645 val = snd_soc_read(widget->codec, e->reg);
1646 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1647 if (e->shift_l != e->shift_r)
1648 ucontrol->value.enumerated.item[1] =
1649 (val >> e->shift_r) & (bitmask - 1);
1650
1651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1654
1655/**
1656 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1657 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001658 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 *
1660 * Callback to set the value of a dapm enumerated double mixer control.
1661 *
1662 * Returns 0 for success.
1663 */
1664int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_value *ucontrol)
1666{
1667 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1668 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001669 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001670 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 int ret = 0;
1672
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001673 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001675 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001676 return -EINVAL;
1677 mux = ucontrol->value.enumerated.item[0];
1678 val = mux << e->shift_l;
1679 mask = (bitmask - 1) << e->shift_l;
1680 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001681 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001682 return -EINVAL;
1683 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1684 mask |= (bitmask - 1) << e->shift_r;
1685 }
1686
1687 mutex_lock(&widget->codec->mutex);
1688 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001689 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1690 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001691
1692 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1693 ret = widget->event(widget,
1694 kcontrol, SND_SOC_DAPM_PRE_REG);
1695 if (ret < 0)
1696 goto out;
1697 }
1698
1699 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1700
1701 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1702 ret = widget->event(widget,
1703 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704
1705out:
1706 mutex_unlock(&widget->codec->mutex);
1707 return ret;
1708}
1709EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1710
1711/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001712 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1713 * @kcontrol: mixer control
1714 * @ucontrol: control element information
1715 *
1716 * Returns 0 for success.
1717 */
1718int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1720{
1721 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1722
1723 ucontrol->value.enumerated.item[0] = widget->value;
1724
1725 return 0;
1726}
1727EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1728
1729/**
1730 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1731 * @kcontrol: mixer control
1732 * @ucontrol: control element information
1733 *
1734 * Returns 0 for success.
1735 */
1736int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
1738{
1739 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1740 struct soc_enum *e =
1741 (struct soc_enum *)kcontrol->private_value;
1742 int change;
1743 int ret = 0;
1744
1745 if (ucontrol->value.enumerated.item[0] >= e->max)
1746 return -EINVAL;
1747
1748 mutex_lock(&widget->codec->mutex);
1749
1750 change = widget->value != ucontrol->value.enumerated.item[0];
1751 widget->value = ucontrol->value.enumerated.item[0];
1752 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1753
1754 mutex_unlock(&widget->codec->mutex);
1755 return ret;
1756}
1757EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1758
1759/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001760 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1761 * callback
1762 * @kcontrol: mixer control
1763 * @ucontrol: control element information
1764 *
1765 * Callback to get the value of a dapm semi enumerated double mixer control.
1766 *
1767 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1768 * used for handling bitfield coded enumeration for example.
1769 *
1770 * Returns 0 for success.
1771 */
1772int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_value *ucontrol)
1774{
1775 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001776 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001777 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001778
1779 reg_val = snd_soc_read(widget->codec, e->reg);
1780 val = (reg_val >> e->shift_l) & e->mask;
1781 for (mux = 0; mux < e->max; mux++) {
1782 if (val == e->values[mux])
1783 break;
1784 }
1785 ucontrol->value.enumerated.item[0] = mux;
1786 if (e->shift_l != e->shift_r) {
1787 val = (reg_val >> e->shift_r) & e->mask;
1788 for (mux = 0; mux < e->max; mux++) {
1789 if (val == e->values[mux])
1790 break;
1791 }
1792 ucontrol->value.enumerated.item[1] = mux;
1793 }
1794
1795 return 0;
1796}
1797EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1798
1799/**
1800 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1801 * callback
1802 * @kcontrol: mixer control
1803 * @ucontrol: control element information
1804 *
1805 * Callback to set the value of a dapm semi enumerated double mixer control.
1806 *
1807 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1808 * used for handling bitfield coded enumeration for example.
1809 *
1810 * Returns 0 for success.
1811 */
1812int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1813 struct snd_ctl_elem_value *ucontrol)
1814{
1815 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001816 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001817 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001818 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001819 int ret = 0;
1820
1821 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1822 return -EINVAL;
1823 mux = ucontrol->value.enumerated.item[0];
1824 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1825 mask = e->mask << e->shift_l;
1826 if (e->shift_l != e->shift_r) {
1827 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1828 return -EINVAL;
1829 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1830 mask |= e->mask << e->shift_r;
1831 }
1832
1833 mutex_lock(&widget->codec->mutex);
1834 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001835 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1836 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001837
1838 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1839 ret = widget->event(widget,
1840 kcontrol, SND_SOC_DAPM_PRE_REG);
1841 if (ret < 0)
1842 goto out;
1843 }
1844
1845 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1846
1847 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1848 ret = widget->event(widget,
1849 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001850
1851out:
1852 mutex_unlock(&widget->codec->mutex);
1853 return ret;
1854}
1855EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1856
1857/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001858 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1859 *
1860 * @kcontrol: mixer control
1861 * @uinfo: control element information
1862 *
1863 * Callback to provide information about a pin switch control.
1864 */
1865int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1866 struct snd_ctl_elem_info *uinfo)
1867{
1868 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1869 uinfo->count = 1;
1870 uinfo->value.integer.min = 0;
1871 uinfo->value.integer.max = 1;
1872
1873 return 0;
1874}
1875EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1876
1877/**
1878 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1879 *
1880 * @kcontrol: mixer control
1881 * @ucontrol: Value
1882 */
1883int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_value *ucontrol)
1885{
1886 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1887 const char *pin = (const char *)kcontrol->private_value;
1888
1889 mutex_lock(&codec->mutex);
1890
1891 ucontrol->value.integer.value[0] =
1892 snd_soc_dapm_get_pin_status(codec, pin);
1893
1894 mutex_unlock(&codec->mutex);
1895
1896 return 0;
1897}
1898EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1899
1900/**
1901 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1902 *
1903 * @kcontrol: mixer control
1904 * @ucontrol: Value
1905 */
1906int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_value *ucontrol)
1908{
1909 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1910 const char *pin = (const char *)kcontrol->private_value;
1911
1912 mutex_lock(&codec->mutex);
1913
1914 if (ucontrol->value.integer.value[0])
1915 snd_soc_dapm_enable_pin(codec, pin);
1916 else
1917 snd_soc_dapm_disable_pin(codec, pin);
1918
1919 snd_soc_dapm_sync(codec);
1920
1921 mutex_unlock(&codec->mutex);
1922
1923 return 0;
1924}
1925EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1926
1927/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928 * snd_soc_dapm_new_control - create new dapm control
1929 * @codec: audio codec
1930 * @widget: widget template
1931 *
1932 * Creates a new dapm control based upon the template.
1933 *
1934 * Returns 0 for success else error.
1935 */
1936int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1937 const struct snd_soc_dapm_widget *widget)
1938{
1939 struct snd_soc_dapm_widget *w;
1940
1941 if ((w = dapm_cnew_widget(widget)) == NULL)
1942 return -ENOMEM;
1943
1944 w->codec = codec;
1945 INIT_LIST_HEAD(&w->sources);
1946 INIT_LIST_HEAD(&w->sinks);
1947 INIT_LIST_HEAD(&w->list);
1948 list_add(&w->list, &codec->dapm_widgets);
1949
1950 /* machine layer set ups unconnected pins and insertions */
1951 w->connected = 1;
1952 return 0;
1953}
1954EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1955
1956/**
Mark Brown4ba13272008-05-13 14:51:19 +02001957 * snd_soc_dapm_new_controls - create new dapm controls
1958 * @codec: audio codec
1959 * @widget: widget array
1960 * @num: number of widgets
1961 *
1962 * Creates new DAPM controls based upon the templates.
1963 *
1964 * Returns 0 for success else error.
1965 */
1966int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1967 const struct snd_soc_dapm_widget *widget,
1968 int num)
1969{
1970 int i, ret;
1971
1972 for (i = 0; i < num; i++) {
1973 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001974 if (ret < 0) {
1975 printk(KERN_ERR
1976 "ASoC: Failed to create DAPM control %s: %d\n",
1977 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001978 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001979 }
Mark Brown4ba13272008-05-13 14:51:19 +02001980 widget++;
1981 }
1982 return 0;
1983}
1984EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1985
1986
1987/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1989 * @codec: audio codec
1990 * @stream: stream name
1991 * @event: stream event
1992 *
1993 * Sends a stream event to the dapm core. The core then makes any
1994 * necessary widget power changes.
1995 *
1996 * Returns 0 for success else error.
1997 */
1998int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1999 char *stream, int event)
2000{
2001 struct snd_soc_dapm_widget *w;
2002
Seth Forshee11da21a2007-02-02 17:14:19 +01002003 if (stream == NULL)
2004 return 0;
2005
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006 mutex_lock(&codec->mutex);
2007 list_for_each_entry(w, &codec->dapm_widgets, list)
2008 {
2009 if (!w->sname)
2010 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002011 pr_debug("widget %s\n %s stream %s event %d\n",
2012 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 if (strstr(w->sname, stream)) {
2014 switch(event) {
2015 case SND_SOC_DAPM_STREAM_START:
2016 w->active = 1;
2017 break;
2018 case SND_SOC_DAPM_STREAM_STOP:
2019 w->active = 0;
2020 break;
2021 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2025 break;
2026 }
2027 }
2028 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002029
2030 dapm_power_widgets(codec, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002031 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032 return 0;
2033}
2034EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2035
2036/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002037 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002038 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002039 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 *
Mark Brown74b8f952009-06-06 11:26:15 +01002041 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002042 * a valid audio route and active audio stream.
2043 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2044 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 */
Mark Brown16499232009-01-07 18:25:13 +00002046int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047{
Liam Girdwooda5302182008-07-07 13:35:17 +01002048 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049}
Liam Girdwooda5302182008-07-07 13:35:17 +01002050EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051
2052/**
Mark Brownda341832010-03-15 19:23:37 +00002053 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2054 * @codec: SoC codec
2055 * @pin: pin name
2056 *
2057 * Enables input/output pin regardless of any other state. This is
2058 * intended for use with microphone bias supplies used in microphone
2059 * jack detection.
2060 *
2061 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2062 * do any widget power switching.
2063 */
2064int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2065{
2066 struct snd_soc_dapm_widget *w;
2067
2068 list_for_each_entry(w, &codec->dapm_widgets, list) {
2069 if (!strcmp(w->name, pin)) {
2070 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2071 w->connected = 1;
2072 w->force = 1;
2073 return 0;
2074 }
2075 }
2076
2077 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2078 return -EINVAL;
2079}
2080EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2081
2082/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002083 * snd_soc_dapm_disable_pin - disable pin.
2084 * @codec: SoC codec
2085 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002086 *
Mark Brown74b8f952009-06-06 11:26:15 +01002087 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002088 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2089 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002090 */
Mark Brown16499232009-01-07 18:25:13 +00002091int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002092{
2093 return snd_soc_dapm_set_pin(codec, pin, 0);
2094}
2095EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2096
2097/**
Mark Brown5817b522008-09-24 11:23:11 +01002098 * snd_soc_dapm_nc_pin - permanently disable pin.
2099 * @codec: SoC codec
2100 * @pin: pin name
2101 *
2102 * Marks the specified pin as being not connected, disabling it along
2103 * any parent or child widgets. At present this is identical to
2104 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2105 * additional things such as disabling controls which only affect
2106 * paths through the pin.
2107 *
2108 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2109 * do any widget power switching.
2110 */
Mark Brown16499232009-01-07 18:25:13 +00002111int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002112{
2113 return snd_soc_dapm_set_pin(codec, pin, 0);
2114}
2115EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2116
2117/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002118 * snd_soc_dapm_get_pin_status - get audio pin status
2119 * @codec: audio codec
2120 * @pin: audio signal pin endpoint (or start point)
2121 *
2122 * Get audio pin status - connected or disconnected.
2123 *
2124 * Returns 1 for connected otherwise 0.
2125 */
Mark Brown16499232009-01-07 18:25:13 +00002126int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002127{
2128 struct snd_soc_dapm_widget *w;
2129
2130 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002131 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002132 return w->connected;
2133 }
2134
2135 return 0;
2136}
Liam Girdwooda5302182008-07-07 13:35:17 +01002137EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002138
2139/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140 * snd_soc_dapm_free - free dapm resources
2141 * @socdev: SoC device
2142 *
2143 * Free all dapm widgets and resources.
2144 */
2145void snd_soc_dapm_free(struct snd_soc_device *socdev)
2146{
Mark Brown6627a652009-01-23 22:55:23 +00002147 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148
2149 snd_soc_dapm_sys_remove(socdev->dev);
2150 dapm_free_widgets(codec);
2151}
2152EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2153
Mark Brown51737472009-06-22 13:16:51 +01002154/*
2155 * snd_soc_dapm_shutdown - callback for system shutdown
2156 */
2157void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2158{
2159 struct snd_soc_codec *codec = socdev->card->codec;
2160 struct snd_soc_dapm_widget *w;
2161 LIST_HEAD(down_list);
2162 int powerdown = 0;
2163
2164 list_for_each_entry(w, &codec->dapm_widgets, list) {
2165 if (w->power) {
2166 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002167 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002168 powerdown = 1;
2169 }
2170 }
2171
2172 /* If there were no widgets to power down we're already in
2173 * standby.
2174 */
2175 if (powerdown) {
2176 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2177 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2178 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2179 }
2180
2181 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2182}
2183
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002185MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2187MODULE_LICENSE("GPL");