blob: fefb6c44fc81dd398496e6c50d3f57b90376d2b9 [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:
Mark Brown1547aba2010-05-07 21:11:40 +0100444 if (widget->ignore_suspend)
445 pr_debug("%s ignoring suspend\n", widget->name);
446 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100447 default:
448 return 1;
449 }
450}
451
Richard Purdie2b97eab2006-10-06 18:32:18 +0200452/*
453 * Recursively check for a completed path to an active or physically connected
454 * output widget. Returns number of complete paths.
455 */
456static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
457{
458 struct snd_soc_dapm_path *path;
459 int con = 0;
460
Mark Brown246d0a12009-04-22 18:24:55 +0100461 if (widget->id == snd_soc_dapm_supply)
462 return 0;
463
Mark Brown010ff262009-08-17 17:39:22 +0100464 switch (widget->id) {
465 case snd_soc_dapm_adc:
466 case snd_soc_dapm_aif_out:
467 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100468 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100469 default:
470 break;
471 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472
473 if (widget->connected) {
474 /* connected pin ? */
475 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100476 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200477
478 /* connected jack or spk ? */
479 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300480 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100481 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200482 }
483
484 list_for_each_entry(path, &widget->sinks, list_source) {
485 if (path->walked)
486 continue;
487
488 if (path->sink && path->connect) {
489 path->walked = 1;
490 con += is_connected_output_ep(path->sink);
491 }
492 }
493
494 return con;
495}
496
497/*
498 * Recursively check for a completed path to an active or physically connected
499 * input widget. Returns number of complete paths.
500 */
501static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
502{
503 struct snd_soc_dapm_path *path;
504 int con = 0;
505
Mark Brown246d0a12009-04-22 18:24:55 +0100506 if (widget->id == snd_soc_dapm_supply)
507 return 0;
508
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100510 switch (widget->id) {
511 case snd_soc_dapm_dac:
512 case snd_soc_dapm_aif_in:
513 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100514 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100515 default:
516 break;
517 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518
519 if (widget->connected) {
520 /* connected pin ? */
521 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100522 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523
524 /* connected VMID/Bias for lower pops */
525 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100526 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527
528 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300529 if (widget->id == snd_soc_dapm_mic ||
530 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100531 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 }
533
534 list_for_each_entry(path, &widget->sources, list_sink) {
535 if (path->walked)
536 continue;
537
538 if (path->source && path->connect) {
539 path->walked = 1;
540 con += is_connected_input_ep(path->source);
541 }
542 }
543
544 return con;
545}
546
547/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300548 * Handler for generic register modifier widget.
549 */
550int dapm_reg_event(struct snd_soc_dapm_widget *w,
551 struct snd_kcontrol *kcontrol, int event)
552{
553 unsigned int val;
554
555 if (SND_SOC_DAPM_EVENT_ON(event))
556 val = w->on_val;
557 else
558 val = w->off_val;
559
560 snd_soc_update_bits(w->codec, -(w->reg + 1),
561 w->mask << w->shift, val << w->shift);
562
563 return 0;
564}
Mark Brown11589412008-07-29 11:42:23 +0100565EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300566
Mark Brown025756e2009-04-13 11:09:18 +0100567/* Standard power change method, used to apply power changes to most
568 * widgets.
569 */
570static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
571{
572 int ret;
573
574 /* call any power change event handlers */
575 if (w->event)
576 pr_debug("power %s event for %s flags %x\n",
577 w->power ? "on" : "off",
578 w->name, w->event_flags);
579
580 /* power up pre event */
581 if (w->power && w->event &&
582 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
583 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
584 if (ret < 0)
585 return ret;
586 }
587
588 /* power down pre event */
589 if (!w->power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
592 if (ret < 0)
593 return ret;
594 }
595
Mark Brown025756e2009-04-13 11:09:18 +0100596 dapm_update_bits(w);
597
Mark Brown025756e2009-04-13 11:09:18 +0100598 /* power up post event */
599 if (w->power && w->event &&
600 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
601 ret = w->event(w,
602 NULL, SND_SOC_DAPM_POST_PMU);
603 if (ret < 0)
604 return ret;
605 }
606
607 /* power down post event */
608 if (!w->power && w->event &&
609 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
610 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
611 if (ret < 0)
612 return ret;
613 }
614
615 return 0;
616}
617
Mark Browncd0f2d42009-04-20 16:56:59 +0100618/* Generic check to see if a widget should be powered.
619 */
620static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
621{
622 int in, out;
623
624 in = is_connected_input_ep(w);
625 dapm_clear_walk(w->codec);
626 out = is_connected_output_ep(w);
627 dapm_clear_walk(w->codec);
628 return out != 0 && in != 0;
629}
630
Mark Brown6ea31b92009-04-20 17:15:41 +0100631/* Check to see if an ADC has power */
632static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
633{
634 int in;
635
636 if (w->active) {
637 in = is_connected_input_ep(w);
638 dapm_clear_walk(w->codec);
639 return in != 0;
640 } else {
641 return dapm_generic_check_power(w);
642 }
643}
644
645/* Check to see if a DAC has power */
646static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
647{
648 int out;
649
650 if (w->active) {
651 out = is_connected_output_ep(w);
652 dapm_clear_walk(w->codec);
653 return out != 0;
654 } else {
655 return dapm_generic_check_power(w);
656 }
657}
658
Mark Brown246d0a12009-04-22 18:24:55 +0100659/* Check to see if a power supply is needed */
660static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
661{
662 struct snd_soc_dapm_path *path;
663 int power = 0;
664
665 /* Check if one of our outputs is connected */
666 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100667 if (path->connected &&
668 !path->connected(path->source, path->sink))
669 continue;
670
Mark Brown246d0a12009-04-22 18:24:55 +0100671 if (path->sink && path->sink->power_check &&
672 path->sink->power_check(path->sink)) {
673 power = 1;
674 break;
675 }
676 }
677
678 dapm_clear_walk(w->codec);
679
680 return power;
681}
682
Mark Brown38357ab2009-06-06 19:03:23 +0100683static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
684 struct snd_soc_dapm_widget *b,
685 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000686{
Mark Brownd207c682009-12-07 17:13:55 +0000687 if (a->codec != b->codec)
688 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100689 if (sort[a->id] != sort[b->id])
690 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100691 if (a->reg != b->reg)
692 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000693
Mark Brown38357ab2009-06-06 19:03:23 +0100694 return 0;
695}
Mark Brown42aa3412009-03-01 19:21:10 +0000696
Mark Brown38357ab2009-06-06 19:03:23 +0100697/* Insert a widget in order into a DAPM power sequence. */
698static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
699 struct list_head *list,
700 int sort[])
701{
702 struct snd_soc_dapm_widget *w;
703
704 list_for_each_entry(w, list, power_list)
705 if (dapm_seq_compare(new_widget, w, sort) < 0) {
706 list_add_tail(&new_widget->power_list, &w->power_list);
707 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000708 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100709
Mark Brown38357ab2009-06-06 19:03:23 +0100710 list_add_tail(&new_widget->power_list, list);
711}
Mark Brown42aa3412009-03-01 19:21:10 +0000712
Mark Brownb22ead22009-06-07 12:51:26 +0100713/* Apply the coalesced changes from a DAPM sequence */
714static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
715 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100716{
717 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100718 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100719 unsigned int value = 0;
720 unsigned int mask = 0;
721 unsigned int cur_mask;
722
723 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
724 power_list)->reg;
725
726 list_for_each_entry(w, pending, power_list) {
727 cur_mask = 1 << w->shift;
728 BUG_ON(reg != w->reg);
729
730 if (w->invert)
731 power = !w->power;
732 else
733 power = w->power;
734
735 mask |= cur_mask;
736 if (power)
737 value |= cur_mask;
738
739 pop_dbg(codec->pop_time,
740 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
741 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100742
743 /* power up pre event */
744 if (w->power && w->event &&
745 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
746 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
747 w->name);
748 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
749 if (ret < 0)
750 pr_err("%s: pre event failed: %d\n",
751 w->name, ret);
752 }
753
754 /* power down pre event */
755 if (!w->power && w->event &&
756 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
757 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
758 w->name);
759 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
760 if (ret < 0)
761 pr_err("%s: pre event failed: %d\n",
762 w->name, ret);
763 }
Mark Brownb22ead22009-06-07 12:51:26 +0100764 }
765
Mark Brown81628102009-06-07 13:21:24 +0100766 if (reg >= 0) {
767 pop_dbg(codec->pop_time,
768 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
769 value, mask, reg, codec->pop_time);
770 pop_wait(codec->pop_time);
771 snd_soc_update_bits(codec, reg, mask, value);
772 }
773
774 list_for_each_entry(w, pending, power_list) {
775 /* power up post event */
776 if (w->power && w->event &&
777 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
778 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
779 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000780 ret = w->event(w,
781 NULL, SND_SOC_DAPM_POST_PMU);
782 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100783 pr_err("%s: post event failed: %d\n",
784 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000785 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100786
Mark Brown81628102009-06-07 13:21:24 +0100787 /* power down post event */
788 if (!w->power && w->event &&
789 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
790 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
791 w->name);
792 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
793 if (ret < 0)
794 pr_err("%s: post event failed: %d\n",
795 w->name, ret);
796 }
Mark Brown42aa3412009-03-01 19:21:10 +0000797 }
Mark Brown42aa3412009-03-01 19:21:10 +0000798}
799
Mark Brownb22ead22009-06-07 12:51:26 +0100800/* Apply a DAPM power sequence.
801 *
802 * We walk over a pre-sorted list of widgets to apply power to. In
803 * order to minimise the number of writes to the device required
804 * multiple widgets will be updated in a single write where possible.
805 * Currently anything that requires more than a single write is not
806 * handled.
807 */
808static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
809 int event, int sort[])
810{
811 struct snd_soc_dapm_widget *w, *n;
812 LIST_HEAD(pending);
813 int cur_sort = -1;
814 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100815 int ret;
816
Mark Brownb22ead22009-06-07 12:51:26 +0100817 list_for_each_entry_safe(w, n, list, power_list) {
818 ret = 0;
819
820 /* Do we need to apply any queued changes? */
821 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
822 if (!list_empty(&pending))
823 dapm_seq_run_coalesced(codec, &pending);
824
825 INIT_LIST_HEAD(&pending);
826 cur_sort = -1;
827 cur_reg = SND_SOC_NOPM;
828 }
829
Mark Brown163cac02009-06-07 10:12:52 +0100830 switch (w->id) {
831 case snd_soc_dapm_pre:
832 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100833 list_for_each_entry_safe_continue(w, n, list,
834 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100835
Mark Brownb22ead22009-06-07 12:51:26 +0100836 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100837 ret = w->event(w,
838 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100839 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100840 ret = w->event(w,
841 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100842 break;
843
844 case snd_soc_dapm_post:
845 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100846 list_for_each_entry_safe_continue(w, n, list,
847 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100848
Mark Brownb22ead22009-06-07 12:51:26 +0100849 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100850 ret = w->event(w,
851 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100852 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100853 ret = w->event(w,
854 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100855 break;
856
857 case snd_soc_dapm_input:
858 case snd_soc_dapm_output:
859 case snd_soc_dapm_hp:
860 case snd_soc_dapm_mic:
861 case snd_soc_dapm_line:
862 case snd_soc_dapm_spk:
863 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100864 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100865 break;
866
867 default:
Mark Brown81628102009-06-07 13:21:24 +0100868 /* Queue it up for application */
869 cur_sort = sort[w->id];
870 cur_reg = w->reg;
871 list_move(&w->power_list, &pending);
872 break;
Mark Brown163cac02009-06-07 10:12:52 +0100873 }
Mark Brownb22ead22009-06-07 12:51:26 +0100874
875 if (ret < 0)
876 pr_err("Failed to apply widget power: %d\n",
877 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100878 }
Mark Brownb22ead22009-06-07 12:51:26 +0100879
880 if (!list_empty(&pending))
881 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100882}
883
Mark Brown42aa3412009-03-01 19:21:10 +0000884/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885 * Scan each dapm widget for complete audio path.
886 * A complete path is a route that has valid endpoints i.e.:-
887 *
888 * o DAC to output pin.
889 * o Input Pin to ADC.
890 * o Input pin to Output pin (bypass, sidetone)
891 * o DAC to ADC (loopback).
892 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100893static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200894{
Mark Brown452c5ea2009-05-17 21:41:23 +0100895 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100897 LIST_HEAD(up_list);
898 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100899 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100900 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100901 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200902
Mark Brown6d3ddc82009-05-16 17:47:29 +0100903 /* Check which widgets we need to power and store them in
904 * lists indicating if they should be powered up or down.
905 */
906 list_for_each_entry(w, &codec->dapm_widgets, list) {
907 switch (w->id) {
908 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100909 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100910 break;
911 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100912 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100913 break;
914
915 default:
916 if (!w->power_check)
917 continue;
918
Mark Brown99497882010-05-07 20:24:05 +0100919 if (!w->force)
920 power = w->power_check(w);
921 else
922 power = 1;
923 if (power)
924 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100925
Mark Brown6d3ddc82009-05-16 17:47:29 +0100926 if (w->power == power)
927 continue;
928
929 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100930 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100931 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100932 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100933
934 w->power = power;
935 break;
936 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937 }
938
Mark Brownb14b76a2009-08-17 11:55:38 +0100939 /* If there are no DAPM widgets then try to figure out power from the
940 * event type.
941 */
942 if (list_empty(&codec->dapm_widgets)) {
943 switch (event) {
944 case SND_SOC_DAPM_STREAM_START:
945 case SND_SOC_DAPM_STREAM_RESUME:
946 sys_power = 1;
947 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000948 case SND_SOC_DAPM_STREAM_SUSPEND:
949 sys_power = 0;
950 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100951 case SND_SOC_DAPM_STREAM_NOP:
Mark Browna96ca332010-01-19 22:49:43 +0000952 switch (codec->bias_level) {
953 case SND_SOC_BIAS_STANDBY:
954 case SND_SOC_BIAS_OFF:
955 sys_power = 0;
956 break;
957 default:
958 sys_power = 1;
959 break;
960 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000961 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100962 default:
963 break;
964 }
965 }
966
Mark Browna96ca332010-01-19 22:49:43 +0000967 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
968 ret = snd_soc_dapm_set_bias_level(socdev,
969 SND_SOC_BIAS_STANDBY);
970 if (ret != 0)
971 pr_err("Failed to turn on bias: %d\n", ret);
972 }
973
Mark Brown452c5ea2009-05-17 21:41:23 +0100974 /* If we're changing to all on or all off then prepare */
975 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
976 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
977 ret = snd_soc_dapm_set_bias_level(socdev,
978 SND_SOC_BIAS_PREPARE);
979 if (ret != 0)
980 pr_err("Failed to prepare bias: %d\n", ret);
981 }
982
Mark Brown6d3ddc82009-05-16 17:47:29 +0100983 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100984 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100985
986 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100987 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988
Mark Brown452c5ea2009-05-17 21:41:23 +0100989 /* If we just powered the last thing off drop to standby bias */
990 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
991 ret = snd_soc_dapm_set_bias_level(socdev,
992 SND_SOC_BIAS_STANDBY);
993 if (ret != 0)
994 pr_err("Failed to apply standby bias: %d\n", ret);
995 }
996
Mark Browna96ca332010-01-19 22:49:43 +0000997 /* If we're in standby and can support bias off then do that */
998 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
999 codec->idle_bias_off) {
1000 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1001 if (ret != 0)
1002 pr_err("Failed to turn off bias: %d\n", ret);
1003 }
1004
Mark Brown452c5ea2009-05-17 21:41:23 +01001005 /* If we just powered up then move to active bias */
1006 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1007 ret = snd_soc_dapm_set_bias_level(socdev,
1008 SND_SOC_BIAS_ON);
1009 if (ret != 0)
1010 pr_err("Failed to apply active bias: %d\n", ret);
1011 }
1012
Mark Browncb507e72009-07-08 18:54:57 +01001013 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1014 codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +00001015 pop_wait(codec->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001016
Mark Brown42aa3412009-03-01 19:21:10 +00001017 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018}
1019
Mark Brown79fb9382009-08-21 16:38:13 +01001020#ifdef CONFIG_DEBUG_FS
1021static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1022{
1023 file->private_data = inode->i_private;
1024 return 0;
1025}
1026
1027static ssize_t dapm_widget_power_read_file(struct file *file,
1028 char __user *user_buf,
1029 size_t count, loff_t *ppos)
1030{
1031 struct snd_soc_dapm_widget *w = file->private_data;
1032 char *buf;
1033 int in, out;
1034 ssize_t ret;
1035 struct snd_soc_dapm_path *p = NULL;
1036
1037 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1038 if (!buf)
1039 return -ENOMEM;
1040
1041 in = is_connected_input_ep(w);
1042 dapm_clear_walk(w->codec);
1043 out = is_connected_output_ep(w);
1044 dapm_clear_walk(w->codec);
1045
Mark Brownd033c362009-12-04 15:25:56 +00001046 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001047 w->name, w->power ? "On" : "Off", in, out);
1048
Mark Brownd033c362009-12-04 15:25:56 +00001049 if (w->reg >= 0)
1050 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1051 " - R%d(0x%x) bit %d",
1052 w->reg, w->reg, w->shift);
1053
1054 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1055
Mark Brown3eef08b2009-09-14 16:49:00 +01001056 if (w->sname)
1057 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1058 w->sname,
1059 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001060
1061 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001062 if (p->connected && !p->connected(w, p->sink))
1063 continue;
1064
Mark Brown79fb9382009-08-21 16:38:13 +01001065 if (p->connect)
1066 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1067 " in %s %s\n",
1068 p->name ? p->name : "static",
1069 p->source->name);
1070 }
1071 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001072 if (p->connected && !p->connected(w, p->sink))
1073 continue;
1074
Mark Brown79fb9382009-08-21 16:38:13 +01001075 if (p->connect)
1076 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1077 " out %s %s\n",
1078 p->name ? p->name : "static",
1079 p->sink->name);
1080 }
1081
1082 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1083
1084 kfree(buf);
1085 return ret;
1086}
1087
1088static const struct file_operations dapm_widget_power_fops = {
1089 .open = dapm_widget_power_open_file,
1090 .read = dapm_widget_power_read_file,
1091};
1092
1093void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1094{
1095 struct snd_soc_dapm_widget *w;
1096 struct dentry *d;
1097
1098 if (!codec->debugfs_dapm)
1099 return;
1100
1101 list_for_each_entry(w, &codec->dapm_widgets, list) {
1102 if (!w->name)
1103 continue;
1104
1105 d = debugfs_create_file(w->name, 0444,
1106 codec->debugfs_dapm, w,
1107 &dapm_widget_power_fops);
1108 if (!d)
1109 printk(KERN_WARNING
1110 "ASoC: Failed to create %s debugfs file\n",
1111 w->name);
1112 }
1113}
1114#else
1115void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1116{
1117}
1118#endif
1119
Richard Purdie2b97eab2006-10-06 18:32:18 +02001120/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001121static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001122 struct snd_kcontrol *kcontrol, int change,
1123 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001124{
1125 struct snd_soc_dapm_path *path;
1126 int found = 0;
1127
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001128 if (widget->id != snd_soc_dapm_mux &&
1129 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001130 return -ENODEV;
1131
Mark Brown3a655772009-10-05 17:23:30 +01001132 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001133 return 0;
1134
1135 /* find dapm widget path assoc with kcontrol */
1136 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1137 if (path->kcontrol != kcontrol)
1138 continue;
1139
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001140 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001141 continue;
1142
1143 found = 1;
1144 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001145 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001146 path->connect = 1; /* new connection */
1147 else
1148 path->connect = 0; /* old connection must be powered down */
1149 }
1150
Mark Brownb91b8fa2010-01-20 18:18:35 +00001151 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001152 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1153
1154 return 0;
1155}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001156
Milan plzik1b075e32008-01-10 14:39:46 +01001157/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001158static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001159 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001160{
1161 struct snd_soc_dapm_path *path;
1162 int found = 0;
1163
Milan plzik1b075e32008-01-10 14:39:46 +01001164 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001165 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001166 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167 return -ENODEV;
1168
Richard Purdie2b97eab2006-10-06 18:32:18 +02001169 /* find dapm widget path assoc with kcontrol */
1170 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1171 if (path->kcontrol != kcontrol)
1172 continue;
1173
1174 /* found, now check type */
1175 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001176 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001177 break;
1178 }
1179
Mark Brownb91b8fa2010-01-20 18:18:35 +00001180 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001181 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1182
1183 return 0;
1184}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001185
1186/* show dapm widget status in sys fs */
1187static ssize_t dapm_widget_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1189{
1190 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001191 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001192 struct snd_soc_dapm_widget *w;
1193 int count = 0;
1194 char *state = "not set";
1195
1196 list_for_each_entry(w, &codec->dapm_widgets, list) {
1197
1198 /* only display widgets that burnm power */
1199 switch (w->id) {
1200 case snd_soc_dapm_hp:
1201 case snd_soc_dapm_mic:
1202 case snd_soc_dapm_spk:
1203 case snd_soc_dapm_line:
1204 case snd_soc_dapm_micbias:
1205 case snd_soc_dapm_dac:
1206 case snd_soc_dapm_adc:
1207 case snd_soc_dapm_pga:
1208 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001209 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001210 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001211 if (w->name)
1212 count += sprintf(buf + count, "%s: %s\n",
1213 w->name, w->power ? "On":"Off");
1214 break;
1215 default:
1216 break;
1217 }
1218 }
1219
Mark Brown0be98982008-05-19 12:31:28 +02001220 switch (codec->bias_level) {
1221 case SND_SOC_BIAS_ON:
1222 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 break;
Mark Brown0be98982008-05-19 12:31:28 +02001224 case SND_SOC_BIAS_PREPARE:
1225 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001226 break;
Mark Brown0be98982008-05-19 12:31:28 +02001227 case SND_SOC_BIAS_STANDBY:
1228 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229 break;
Mark Brown0be98982008-05-19 12:31:28 +02001230 case SND_SOC_BIAS_OFF:
1231 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001232 break;
1233 }
1234 count += sprintf(buf + count, "PM State: %s\n", state);
1235
1236 return count;
1237}
1238
1239static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1240
1241int snd_soc_dapm_sys_add(struct device *dev)
1242{
Troy Kisky12ef1932008-10-13 17:42:14 -07001243 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001244}
1245
1246static void snd_soc_dapm_sys_remove(struct device *dev)
1247{
Mark Brownaef90842009-05-16 17:53:16 +01001248 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249}
1250
1251/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001252static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001253{
1254 struct snd_soc_dapm_widget *w, *next_w;
1255 struct snd_soc_dapm_path *p, *next_p;
1256
1257 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1258 list_del(&w->list);
1259 kfree(w);
1260 }
1261
1262 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1263 list_del(&p->list);
1264 kfree(p->long_name);
1265 kfree(p);
1266 }
1267}
1268
Liam Girdwooda5302182008-07-07 13:35:17 +01001269static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001270 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001271{
1272 struct snd_soc_dapm_widget *w;
1273
1274 list_for_each_entry(w, &codec->dapm_widgets, list) {
1275 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001276 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001277 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001278 /* Allow disabling of forced pins */
1279 if (status == 0)
1280 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001281 return 0;
1282 }
1283 }
1284
Mark Brownc1286b82008-07-07 19:26:03 +01001285 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001286 return -EINVAL;
1287}
1288
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001290 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001291 * @codec: audio codec
1292 *
1293 * Walks all dapm audio paths and powers widgets according to their
1294 * stream or path usage.
1295 *
1296 * Returns 0 for success.
1297 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001298int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299{
Mark Brownb91b8fa2010-01-20 18:18:35 +00001300 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301}
Liam Girdwooda5302182008-07-07 13:35:17 +01001302EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001303
Mark Brown105f1c22008-05-13 14:52:19 +02001304static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001305 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001306{
1307 struct snd_soc_dapm_path *path;
1308 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001309 const char *sink = route->sink;
1310 const char *control = route->control;
1311 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312 int ret = 0;
1313
1314 /* find src and dest widgets */
1315 list_for_each_entry(w, &codec->dapm_widgets, list) {
1316
1317 if (!wsink && !(strcmp(w->name, sink))) {
1318 wsink = w;
1319 continue;
1320 }
1321 if (!wsource && !(strcmp(w->name, source))) {
1322 wsource = w;
1323 }
1324 }
1325
1326 if (wsource == NULL || wsink == NULL)
1327 return -ENODEV;
1328
1329 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1330 if (!path)
1331 return -ENOMEM;
1332
1333 path->source = wsource;
1334 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001335 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001336 INIT_LIST_HEAD(&path->list);
1337 INIT_LIST_HEAD(&path->list_source);
1338 INIT_LIST_HEAD(&path->list_sink);
1339
1340 /* check for external widgets */
1341 if (wsink->id == snd_soc_dapm_input) {
1342 if (wsource->id == snd_soc_dapm_micbias ||
1343 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001344 wsource->id == snd_soc_dapm_line ||
1345 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001346 wsink->ext = 1;
1347 }
1348 if (wsource->id == snd_soc_dapm_output) {
1349 if (wsink->id == snd_soc_dapm_spk ||
1350 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001351 wsink->id == snd_soc_dapm_line ||
1352 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001353 wsource->ext = 1;
1354 }
1355
1356 /* connect static paths */
1357 if (control == NULL) {
1358 list_add(&path->list, &codec->dapm_paths);
1359 list_add(&path->list_sink, &wsink->sources);
1360 list_add(&path->list_source, &wsource->sinks);
1361 path->connect = 1;
1362 return 0;
1363 }
1364
1365 /* connect dynamic paths */
1366 switch(wsink->id) {
1367 case snd_soc_dapm_adc:
1368 case snd_soc_dapm_dac:
1369 case snd_soc_dapm_pga:
1370 case snd_soc_dapm_input:
1371 case snd_soc_dapm_output:
1372 case snd_soc_dapm_micbias:
1373 case snd_soc_dapm_vmid:
1374 case snd_soc_dapm_pre:
1375 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001376 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001377 case snd_soc_dapm_aif_in:
1378 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001379 list_add(&path->list, &codec->dapm_paths);
1380 list_add(&path->list_sink, &wsink->sources);
1381 list_add(&path->list_source, &wsource->sinks);
1382 path->connect = 1;
1383 return 0;
1384 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001385 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001386 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1387 &wsink->kcontrols[0]);
1388 if (ret != 0)
1389 goto err;
1390 break;
1391 case snd_soc_dapm_switch:
1392 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001393 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001394 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1395 if (ret != 0)
1396 goto err;
1397 break;
1398 case snd_soc_dapm_hp:
1399 case snd_soc_dapm_mic:
1400 case snd_soc_dapm_line:
1401 case snd_soc_dapm_spk:
1402 list_add(&path->list, &codec->dapm_paths);
1403 list_add(&path->list_sink, &wsink->sources);
1404 list_add(&path->list_source, &wsource->sinks);
1405 path->connect = 0;
1406 return 0;
1407 }
1408 return 0;
1409
1410err:
1411 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1412 control, sink);
1413 kfree(path);
1414 return ret;
1415}
Mark Brown105f1c22008-05-13 14:52:19 +02001416
1417/**
Mark Brown105f1c22008-05-13 14:52:19 +02001418 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1419 * @codec: codec
1420 * @route: audio routes
1421 * @num: number of routes
1422 *
1423 * Connects 2 dapm widgets together via a named audio path. The sink is
1424 * the widget receiving the audio signal, whilst the source is the sender
1425 * of the audio signal.
1426 *
1427 * Returns 0 for success else error. On error all resources can be freed
1428 * with a call to snd_soc_card_free().
1429 */
1430int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1431 const struct snd_soc_dapm_route *route, int num)
1432{
1433 int i, ret;
1434
1435 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001436 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001437 if (ret < 0) {
1438 printk(KERN_ERR "Failed to add route %s->%s\n",
1439 route->source,
1440 route->sink);
1441 return ret;
1442 }
1443 route++;
1444 }
1445
1446 return 0;
1447}
1448EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1449
1450/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001451 * snd_soc_dapm_new_widgets - add new dapm widgets
1452 * @codec: audio codec
1453 *
1454 * Checks the codec for any new dapm widgets and creates them if found.
1455 *
1456 * Returns 0 for success.
1457 */
1458int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1459{
1460 struct snd_soc_dapm_widget *w;
1461
Richard Purdie2b97eab2006-10-06 18:32:18 +02001462 list_for_each_entry(w, &codec->dapm_widgets, list)
1463 {
1464 if (w->new)
1465 continue;
1466
1467 switch(w->id) {
1468 case snd_soc_dapm_switch:
1469 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001470 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001471 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001472 dapm_new_mixer(codec, w);
1473 break;
1474 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001475 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001476 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001477 dapm_new_mux(codec, w);
1478 break;
1479 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001480 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001481 w->power_check = dapm_adc_check_power;
1482 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001483 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001484 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001485 w->power_check = dapm_dac_check_power;
1486 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001487 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001488 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001489 dapm_new_pga(codec, w);
1490 break;
1491 case snd_soc_dapm_input:
1492 case snd_soc_dapm_output:
1493 case snd_soc_dapm_micbias:
1494 case snd_soc_dapm_spk:
1495 case snd_soc_dapm_hp:
1496 case snd_soc_dapm_mic:
1497 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001498 w->power_check = dapm_generic_check_power;
1499 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001500 case snd_soc_dapm_supply:
1501 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001502 case snd_soc_dapm_vmid:
1503 case snd_soc_dapm_pre:
1504 case snd_soc_dapm_post:
1505 break;
1506 }
1507 w->new = 1;
1508 }
1509
1510 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001511 return 0;
1512}
1513EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1514
1515/**
1516 * snd_soc_dapm_get_volsw - dapm mixer get callback
1517 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001518 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 *
1520 * Callback to get the value of a dapm mixer control.
1521 *
1522 * Returns 0 for success.
1523 */
1524int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_value *ucontrol)
1526{
1527 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001528 struct soc_mixer_control *mc =
1529 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001530 unsigned int reg = mc->reg;
1531 unsigned int shift = mc->shift;
1532 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001533 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001534 unsigned int invert = mc->invert;
1535 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537 ucontrol->value.integer.value[0] =
1538 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1539 if (shift != rshift)
1540 ucontrol->value.integer.value[1] =
1541 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1542 if (invert) {
1543 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001544 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 if (shift != rshift)
1546 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001547 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001548 }
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1553
1554/**
1555 * snd_soc_dapm_put_volsw - dapm mixer set callback
1556 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001557 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 *
1559 * Callback to set the value of a dapm mixer control.
1560 *
1561 * Returns 0 for success.
1562 */
1563int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1564 struct snd_ctl_elem_value *ucontrol)
1565{
1566 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001567 struct soc_mixer_control *mc =
1568 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001569 unsigned int reg = mc->reg;
1570 unsigned int shift = mc->shift;
1571 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001572 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001573 unsigned int mask = (1 << fls(max)) - 1;
1574 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001575 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001576 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577 int ret;
1578
1579 val = (ucontrol->value.integer.value[0] & mask);
1580
1581 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001582 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001583 val_mask = mask << shift;
1584 val = val << shift;
1585 if (shift != rshift) {
1586 val2 = (ucontrol->value.integer.value[1] & mask);
1587 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001588 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001589 val_mask |= mask << rshift;
1590 val |= val2 << rshift;
1591 }
1592
1593 mutex_lock(&widget->codec->mutex);
1594 widget->value = val;
1595
Mark Brown283375c2009-12-07 18:09:03 +00001596 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1597 if (val)
1598 /* new connection */
1599 connect = invert ? 0:1;
1600 else
1601 /* old connection must be powered down */
1602 connect = invert ? 1:0;
1603
1604 dapm_mixer_update_power(widget, kcontrol, connect);
1605 }
1606
Richard Purdie2b97eab2006-10-06 18:32:18 +02001607 if (widget->event) {
1608 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001609 ret = widget->event(widget, kcontrol,
1610 SND_SOC_DAPM_PRE_REG);
1611 if (ret < 0) {
1612 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001614 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001615 }
1616 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1617 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001618 ret = widget->event(widget, kcontrol,
1619 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 } else
1621 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1622
1623out:
1624 mutex_unlock(&widget->codec->mutex);
1625 return ret;
1626}
1627EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1628
1629/**
1630 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1631 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001632 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633 *
1634 * Callback to get the value of a dapm enumerated double mixer control.
1635 *
1636 * Returns 0 for success.
1637 */
1638int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
1640{
1641 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1642 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001643 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001645 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646 ;
1647 val = snd_soc_read(widget->codec, e->reg);
1648 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1649 if (e->shift_l != e->shift_r)
1650 ucontrol->value.enumerated.item[1] =
1651 (val >> e->shift_r) & (bitmask - 1);
1652
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1656
1657/**
1658 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1659 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001660 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661 *
1662 * Callback to set the value of a dapm enumerated double mixer control.
1663 *
1664 * Returns 0 for success.
1665 */
1666int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol)
1668{
1669 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1670 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001671 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001672 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001673 int ret = 0;
1674
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001675 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001676 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001677 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001678 return -EINVAL;
1679 mux = ucontrol->value.enumerated.item[0];
1680 val = mux << e->shift_l;
1681 mask = (bitmask - 1) << e->shift_l;
1682 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001683 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684 return -EINVAL;
1685 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1686 mask |= (bitmask - 1) << e->shift_r;
1687 }
1688
1689 mutex_lock(&widget->codec->mutex);
1690 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001691 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1692 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001693
1694 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1695 ret = widget->event(widget,
1696 kcontrol, SND_SOC_DAPM_PRE_REG);
1697 if (ret < 0)
1698 goto out;
1699 }
1700
1701 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1702
1703 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1704 ret = widget->event(widget,
1705 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706
1707out:
1708 mutex_unlock(&widget->codec->mutex);
1709 return ret;
1710}
1711EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1712
1713/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001714 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1715 * @kcontrol: mixer control
1716 * @ucontrol: control element information
1717 *
1718 * Returns 0 for success.
1719 */
1720int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1724
1725 ucontrol->value.enumerated.item[0] = widget->value;
1726
1727 return 0;
1728}
1729EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1730
1731/**
1732 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1733 * @kcontrol: mixer control
1734 * @ucontrol: control element information
1735 *
1736 * Returns 0 for success.
1737 */
1738int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol)
1740{
1741 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1742 struct soc_enum *e =
1743 (struct soc_enum *)kcontrol->private_value;
1744 int change;
1745 int ret = 0;
1746
1747 if (ucontrol->value.enumerated.item[0] >= e->max)
1748 return -EINVAL;
1749
1750 mutex_lock(&widget->codec->mutex);
1751
1752 change = widget->value != ucontrol->value.enumerated.item[0];
1753 widget->value = ucontrol->value.enumerated.item[0];
1754 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1755
1756 mutex_unlock(&widget->codec->mutex);
1757 return ret;
1758}
1759EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1760
1761/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001762 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1763 * callback
1764 * @kcontrol: mixer control
1765 * @ucontrol: control element information
1766 *
1767 * Callback to get the value of a dapm semi enumerated double mixer control.
1768 *
1769 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1770 * used for handling bitfield coded enumeration for example.
1771 *
1772 * Returns 0 for success.
1773 */
1774int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_value *ucontrol)
1776{
1777 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001778 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001779 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001780
1781 reg_val = snd_soc_read(widget->codec, e->reg);
1782 val = (reg_val >> e->shift_l) & e->mask;
1783 for (mux = 0; mux < e->max; mux++) {
1784 if (val == e->values[mux])
1785 break;
1786 }
1787 ucontrol->value.enumerated.item[0] = mux;
1788 if (e->shift_l != e->shift_r) {
1789 val = (reg_val >> e->shift_r) & e->mask;
1790 for (mux = 0; mux < e->max; mux++) {
1791 if (val == e->values[mux])
1792 break;
1793 }
1794 ucontrol->value.enumerated.item[1] = mux;
1795 }
1796
1797 return 0;
1798}
1799EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1800
1801/**
1802 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1803 * callback
1804 * @kcontrol: mixer control
1805 * @ucontrol: control element information
1806 *
1807 * Callback to set the value of a dapm semi enumerated double mixer control.
1808 *
1809 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1810 * used for handling bitfield coded enumeration for example.
1811 *
1812 * Returns 0 for success.
1813 */
1814int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
1816{
1817 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001818 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001819 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001820 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001821 int ret = 0;
1822
1823 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1824 return -EINVAL;
1825 mux = ucontrol->value.enumerated.item[0];
1826 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1827 mask = e->mask << e->shift_l;
1828 if (e->shift_l != e->shift_r) {
1829 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1830 return -EINVAL;
1831 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1832 mask |= e->mask << e->shift_r;
1833 }
1834
1835 mutex_lock(&widget->codec->mutex);
1836 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001837 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1838 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001839
1840 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1841 ret = widget->event(widget,
1842 kcontrol, SND_SOC_DAPM_PRE_REG);
1843 if (ret < 0)
1844 goto out;
1845 }
1846
1847 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1848
1849 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1850 ret = widget->event(widget,
1851 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001852
1853out:
1854 mutex_unlock(&widget->codec->mutex);
1855 return ret;
1856}
1857EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1858
1859/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001860 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1861 *
1862 * @kcontrol: mixer control
1863 * @uinfo: control element information
1864 *
1865 * Callback to provide information about a pin switch control.
1866 */
1867int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_info *uinfo)
1869{
1870 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1871 uinfo->count = 1;
1872 uinfo->value.integer.min = 0;
1873 uinfo->value.integer.max = 1;
1874
1875 return 0;
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1878
1879/**
1880 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1881 *
1882 * @kcontrol: mixer control
1883 * @ucontrol: Value
1884 */
1885int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1886 struct snd_ctl_elem_value *ucontrol)
1887{
1888 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1889 const char *pin = (const char *)kcontrol->private_value;
1890
1891 mutex_lock(&codec->mutex);
1892
1893 ucontrol->value.integer.value[0] =
1894 snd_soc_dapm_get_pin_status(codec, pin);
1895
1896 mutex_unlock(&codec->mutex);
1897
1898 return 0;
1899}
1900EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1901
1902/**
1903 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1904 *
1905 * @kcontrol: mixer control
1906 * @ucontrol: Value
1907 */
1908int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1910{
1911 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1912 const char *pin = (const char *)kcontrol->private_value;
1913
1914 mutex_lock(&codec->mutex);
1915
1916 if (ucontrol->value.integer.value[0])
1917 snd_soc_dapm_enable_pin(codec, pin);
1918 else
1919 snd_soc_dapm_disable_pin(codec, pin);
1920
1921 snd_soc_dapm_sync(codec);
1922
1923 mutex_unlock(&codec->mutex);
1924
1925 return 0;
1926}
1927EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1928
1929/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930 * snd_soc_dapm_new_control - create new dapm control
1931 * @codec: audio codec
1932 * @widget: widget template
1933 *
1934 * Creates a new dapm control based upon the template.
1935 *
1936 * Returns 0 for success else error.
1937 */
1938int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1939 const struct snd_soc_dapm_widget *widget)
1940{
1941 struct snd_soc_dapm_widget *w;
1942
1943 if ((w = dapm_cnew_widget(widget)) == NULL)
1944 return -ENOMEM;
1945
1946 w->codec = codec;
1947 INIT_LIST_HEAD(&w->sources);
1948 INIT_LIST_HEAD(&w->sinks);
1949 INIT_LIST_HEAD(&w->list);
1950 list_add(&w->list, &codec->dapm_widgets);
1951
1952 /* machine layer set ups unconnected pins and insertions */
1953 w->connected = 1;
1954 return 0;
1955}
1956EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1957
1958/**
Mark Brown4ba13272008-05-13 14:51:19 +02001959 * snd_soc_dapm_new_controls - create new dapm controls
1960 * @codec: audio codec
1961 * @widget: widget array
1962 * @num: number of widgets
1963 *
1964 * Creates new DAPM controls based upon the templates.
1965 *
1966 * Returns 0 for success else error.
1967 */
1968int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1969 const struct snd_soc_dapm_widget *widget,
1970 int num)
1971{
1972 int i, ret;
1973
1974 for (i = 0; i < num; i++) {
1975 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001976 if (ret < 0) {
1977 printk(KERN_ERR
1978 "ASoC: Failed to create DAPM control %s: %d\n",
1979 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001980 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001981 }
Mark Brown4ba13272008-05-13 14:51:19 +02001982 widget++;
1983 }
1984 return 0;
1985}
1986EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1987
1988
1989/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1991 * @codec: audio codec
1992 * @stream: stream name
1993 * @event: stream event
1994 *
1995 * Sends a stream event to the dapm core. The core then makes any
1996 * necessary widget power changes.
1997 *
1998 * Returns 0 for success else error.
1999 */
2000int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2001 char *stream, int event)
2002{
2003 struct snd_soc_dapm_widget *w;
2004
Seth Forshee11da21a2007-02-02 17:14:19 +01002005 if (stream == NULL)
2006 return 0;
2007
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 mutex_lock(&codec->mutex);
2009 list_for_each_entry(w, &codec->dapm_widgets, list)
2010 {
2011 if (!w->sname)
2012 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002013 pr_debug("widget %s\n %s stream %s event %d\n",
2014 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 if (strstr(w->sname, stream)) {
2016 switch(event) {
2017 case SND_SOC_DAPM_STREAM_START:
2018 w->active = 1;
2019 break;
2020 case SND_SOC_DAPM_STREAM_STOP:
2021 w->active = 0;
2022 break;
2023 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2027 break;
2028 }
2029 }
2030 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002031
2032 dapm_power_widgets(codec, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002033 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 return 0;
2035}
2036EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2037
2038/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002039 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00002040 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01002041 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 *
Mark Brown74b8f952009-06-06 11:26:15 +01002043 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002044 * a valid audio route and active audio stream.
2045 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2046 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 */
Mark Brown16499232009-01-07 18:25:13 +00002048int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049{
Liam Girdwooda5302182008-07-07 13:35:17 +01002050 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051}
Liam Girdwooda5302182008-07-07 13:35:17 +01002052EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053
2054/**
Mark Brownda341832010-03-15 19:23:37 +00002055 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2056 * @codec: SoC codec
2057 * @pin: pin name
2058 *
2059 * Enables input/output pin regardless of any other state. This is
2060 * intended for use with microphone bias supplies used in microphone
2061 * jack detection.
2062 *
2063 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2064 * do any widget power switching.
2065 */
2066int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2067{
2068 struct snd_soc_dapm_widget *w;
2069
2070 list_for_each_entry(w, &codec->dapm_widgets, list) {
2071 if (!strcmp(w->name, pin)) {
2072 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2073 w->connected = 1;
2074 w->force = 1;
2075 return 0;
2076 }
2077 }
2078
2079 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2080 return -EINVAL;
2081}
2082EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2083
2084/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002085 * snd_soc_dapm_disable_pin - disable pin.
2086 * @codec: SoC codec
2087 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002088 *
Mark Brown74b8f952009-06-06 11:26:15 +01002089 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002090 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2091 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002092 */
Mark Brown16499232009-01-07 18:25:13 +00002093int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002094{
2095 return snd_soc_dapm_set_pin(codec, pin, 0);
2096}
2097EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2098
2099/**
Mark Brown5817b522008-09-24 11:23:11 +01002100 * snd_soc_dapm_nc_pin - permanently disable pin.
2101 * @codec: SoC codec
2102 * @pin: pin name
2103 *
2104 * Marks the specified pin as being not connected, disabling it along
2105 * any parent or child widgets. At present this is identical to
2106 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2107 * additional things such as disabling controls which only affect
2108 * paths through the pin.
2109 *
2110 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2111 * do any widget power switching.
2112 */
Mark Brown16499232009-01-07 18:25:13 +00002113int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002114{
2115 return snd_soc_dapm_set_pin(codec, pin, 0);
2116}
2117EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2118
2119/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002120 * snd_soc_dapm_get_pin_status - get audio pin status
2121 * @codec: audio codec
2122 * @pin: audio signal pin endpoint (or start point)
2123 *
2124 * Get audio pin status - connected or disconnected.
2125 *
2126 * Returns 1 for connected otherwise 0.
2127 */
Mark Brown16499232009-01-07 18:25:13 +00002128int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002129{
2130 struct snd_soc_dapm_widget *w;
2131
2132 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002133 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002134 return w->connected;
2135 }
2136
2137 return 0;
2138}
Liam Girdwooda5302182008-07-07 13:35:17 +01002139EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002140
2141/**
Mark Brown1547aba2010-05-07 21:11:40 +01002142 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2143 * @codec: audio codec
2144 * @pin: audio signal pin endpoint (or start point)
2145 *
2146 * Mark the given endpoint or pin as ignoring suspend. When the
2147 * system is disabled a path between two endpoints flagged as ignoring
2148 * suspend will not be disabled. The path must already be enabled via
2149 * normal means at suspend time, it will not be turned on if it was not
2150 * already enabled.
2151 */
2152int snd_soc_dapm_ignore_suspend(struct snd_soc_codec *codec, const char *pin)
2153{
2154 struct snd_soc_dapm_widget *w;
2155
2156 list_for_each_entry(w, &codec->dapm_widgets, list) {
2157 if (!strcmp(w->name, pin)) {
2158 w->ignore_suspend = 1;
2159 return 0;
2160 }
2161 }
2162
2163 pr_err("Unknown DAPM pin: %s\n", pin);
2164 return -EINVAL;
2165}
2166EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2167
2168/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169 * snd_soc_dapm_free - free dapm resources
2170 * @socdev: SoC device
2171 *
2172 * Free all dapm widgets and resources.
2173 */
2174void snd_soc_dapm_free(struct snd_soc_device *socdev)
2175{
Mark Brown6627a652009-01-23 22:55:23 +00002176 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177
2178 snd_soc_dapm_sys_remove(socdev->dev);
2179 dapm_free_widgets(codec);
2180}
2181EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2182
Mark Brown51737472009-06-22 13:16:51 +01002183/*
2184 * snd_soc_dapm_shutdown - callback for system shutdown
2185 */
2186void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2187{
2188 struct snd_soc_codec *codec = socdev->card->codec;
2189 struct snd_soc_dapm_widget *w;
2190 LIST_HEAD(down_list);
2191 int powerdown = 0;
2192
2193 list_for_each_entry(w, &codec->dapm_widgets, list) {
2194 if (w->power) {
2195 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002196 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002197 powerdown = 1;
2198 }
2199 }
2200
2201 /* If there were no widgets to power down we're already in
2202 * standby.
2203 */
2204 if (powerdown) {
2205 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2206 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2207 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2208 }
2209
2210 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2211}
2212
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002214MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2216MODULE_LICENSE("GPL");