blob: 611284194000e5d32afbe636469c572f07f9f5a2 [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
433/*
434 * Recursively check for a completed path to an active or physically connected
435 * output widget. Returns number of complete paths.
436 */
437static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
438{
439 struct snd_soc_dapm_path *path;
440 int con = 0;
441
Mark Brown246d0a12009-04-22 18:24:55 +0100442 if (widget->id == snd_soc_dapm_supply)
443 return 0;
444
Mark Brown010ff262009-08-17 17:39:22 +0100445 switch (widget->id) {
446 case snd_soc_dapm_adc:
447 case snd_soc_dapm_aif_out:
448 if (widget->active)
449 return 1;
450 default:
451 break;
452 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453
454 if (widget->connected) {
455 /* connected pin ? */
456 if (widget->id == snd_soc_dapm_output && !widget->ext)
457 return 1;
458
459 /* connected jack or spk ? */
460 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300461 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200462 return 1;
463 }
464
465 list_for_each_entry(path, &widget->sinks, list_source) {
466 if (path->walked)
467 continue;
468
469 if (path->sink && path->connect) {
470 path->walked = 1;
471 con += is_connected_output_ep(path->sink);
472 }
473 }
474
475 return con;
476}
477
478/*
479 * Recursively check for a completed path to an active or physically connected
480 * input widget. Returns number of complete paths.
481 */
482static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
483{
484 struct snd_soc_dapm_path *path;
485 int con = 0;
486
Mark Brown246d0a12009-04-22 18:24:55 +0100487 if (widget->id == snd_soc_dapm_supply)
488 return 0;
489
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100491 switch (widget->id) {
492 case snd_soc_dapm_dac:
493 case snd_soc_dapm_aif_in:
494 if (widget->active)
495 return 1;
496 default:
497 break;
498 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499
500 if (widget->connected) {
501 /* connected pin ? */
502 if (widget->id == snd_soc_dapm_input && !widget->ext)
503 return 1;
504
505 /* connected VMID/Bias for lower pops */
506 if (widget->id == snd_soc_dapm_vmid)
507 return 1;
508
509 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300510 if (widget->id == snd_soc_dapm_mic ||
511 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512 return 1;
513 }
514
515 list_for_each_entry(path, &widget->sources, list_sink) {
516 if (path->walked)
517 continue;
518
519 if (path->source && path->connect) {
520 path->walked = 1;
521 con += is_connected_input_ep(path->source);
522 }
523 }
524
525 return con;
526}
527
528/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300529 * Handler for generic register modifier widget.
530 */
531int dapm_reg_event(struct snd_soc_dapm_widget *w,
532 struct snd_kcontrol *kcontrol, int event)
533{
534 unsigned int val;
535
536 if (SND_SOC_DAPM_EVENT_ON(event))
537 val = w->on_val;
538 else
539 val = w->off_val;
540
541 snd_soc_update_bits(w->codec, -(w->reg + 1),
542 w->mask << w->shift, val << w->shift);
543
544 return 0;
545}
Mark Brown11589412008-07-29 11:42:23 +0100546EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300547
Mark Brown025756e2009-04-13 11:09:18 +0100548/* Standard power change method, used to apply power changes to most
549 * widgets.
550 */
551static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
552{
553 int ret;
554
555 /* call any power change event handlers */
556 if (w->event)
557 pr_debug("power %s event for %s flags %x\n",
558 w->power ? "on" : "off",
559 w->name, w->event_flags);
560
561 /* power up pre event */
562 if (w->power && w->event &&
563 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
564 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
565 if (ret < 0)
566 return ret;
567 }
568
569 /* power down pre event */
570 if (!w->power && w->event &&
571 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
572 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
573 if (ret < 0)
574 return ret;
575 }
576
Mark Brown025756e2009-04-13 11:09:18 +0100577 dapm_update_bits(w);
578
Mark Brown025756e2009-04-13 11:09:18 +0100579 /* power up post event */
580 if (w->power && w->event &&
581 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
582 ret = w->event(w,
583 NULL, SND_SOC_DAPM_POST_PMU);
584 if (ret < 0)
585 return ret;
586 }
587
588 /* power down post event */
589 if (!w->power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
592 if (ret < 0)
593 return ret;
594 }
595
596 return 0;
597}
598
Mark Browncd0f2d42009-04-20 16:56:59 +0100599/* Generic check to see if a widget should be powered.
600 */
601static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
602{
603 int in, out;
604
605 in = is_connected_input_ep(w);
606 dapm_clear_walk(w->codec);
607 out = is_connected_output_ep(w);
608 dapm_clear_walk(w->codec);
609 return out != 0 && in != 0;
610}
611
Mark Brown6ea31b92009-04-20 17:15:41 +0100612/* Check to see if an ADC has power */
613static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
614{
615 int in;
616
617 if (w->active) {
618 in = is_connected_input_ep(w);
619 dapm_clear_walk(w->codec);
620 return in != 0;
621 } else {
622 return dapm_generic_check_power(w);
623 }
624}
625
626/* Check to see if a DAC has power */
627static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
628{
629 int out;
630
631 if (w->active) {
632 out = is_connected_output_ep(w);
633 dapm_clear_walk(w->codec);
634 return out != 0;
635 } else {
636 return dapm_generic_check_power(w);
637 }
638}
639
Mark Brown246d0a12009-04-22 18:24:55 +0100640/* Check to see if a power supply is needed */
641static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
642{
643 struct snd_soc_dapm_path *path;
644 int power = 0;
645
646 /* Check if one of our outputs is connected */
647 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100648 if (path->connected &&
649 !path->connected(path->source, path->sink))
650 continue;
651
Mark Brown246d0a12009-04-22 18:24:55 +0100652 if (path->sink && path->sink->power_check &&
653 path->sink->power_check(path->sink)) {
654 power = 1;
655 break;
656 }
657 }
658
659 dapm_clear_walk(w->codec);
660
661 return power;
662}
663
Mark Brown38357ab2009-06-06 19:03:23 +0100664static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
665 struct snd_soc_dapm_widget *b,
666 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000667{
Mark Brownd207c682009-12-07 17:13:55 +0000668 if (a->codec != b->codec)
669 return (unsigned long)a - (unsigned long)b;
Mark Brown38357ab2009-06-06 19:03:23 +0100670 if (sort[a->id] != sort[b->id])
671 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100672 if (a->reg != b->reg)
673 return a->reg - b->reg;
Mark Brown42aa3412009-03-01 19:21:10 +0000674
Mark Brown38357ab2009-06-06 19:03:23 +0100675 return 0;
676}
Mark Brown42aa3412009-03-01 19:21:10 +0000677
Mark Brown38357ab2009-06-06 19:03:23 +0100678/* Insert a widget in order into a DAPM power sequence. */
679static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
680 struct list_head *list,
681 int sort[])
682{
683 struct snd_soc_dapm_widget *w;
684
685 list_for_each_entry(w, list, power_list)
686 if (dapm_seq_compare(new_widget, w, sort) < 0) {
687 list_add_tail(&new_widget->power_list, &w->power_list);
688 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000689 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100690
Mark Brown38357ab2009-06-06 19:03:23 +0100691 list_add_tail(&new_widget->power_list, list);
692}
Mark Brown42aa3412009-03-01 19:21:10 +0000693
Mark Brownb22ead22009-06-07 12:51:26 +0100694/* Apply the coalesced changes from a DAPM sequence */
695static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
696 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100697{
698 struct snd_soc_dapm_widget *w;
Mark Brown81628102009-06-07 13:21:24 +0100699 int reg, power, ret;
Mark Brownb22ead22009-06-07 12:51:26 +0100700 unsigned int value = 0;
701 unsigned int mask = 0;
702 unsigned int cur_mask;
703
704 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
705 power_list)->reg;
706
707 list_for_each_entry(w, pending, power_list) {
708 cur_mask = 1 << w->shift;
709 BUG_ON(reg != w->reg);
710
711 if (w->invert)
712 power = !w->power;
713 else
714 power = w->power;
715
716 mask |= cur_mask;
717 if (power)
718 value |= cur_mask;
719
720 pop_dbg(codec->pop_time,
721 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
722 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100723
724 /* power up pre event */
725 if (w->power && w->event &&
726 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
727 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
728 w->name);
729 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
730 if (ret < 0)
731 pr_err("%s: pre event failed: %d\n",
732 w->name, ret);
733 }
734
735 /* power down pre event */
736 if (!w->power && w->event &&
737 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
738 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
739 w->name);
740 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
741 if (ret < 0)
742 pr_err("%s: pre event failed: %d\n",
743 w->name, ret);
744 }
Mark Brownb22ead22009-06-07 12:51:26 +0100745 }
746
Mark Brown81628102009-06-07 13:21:24 +0100747 if (reg >= 0) {
748 pop_dbg(codec->pop_time,
749 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
750 value, mask, reg, codec->pop_time);
751 pop_wait(codec->pop_time);
752 snd_soc_update_bits(codec, reg, mask, value);
753 }
754
755 list_for_each_entry(w, pending, power_list) {
756 /* power up post event */
757 if (w->power && w->event &&
758 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
759 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
760 w->name);
Mark Brown42aa3412009-03-01 19:21:10 +0000761 ret = w->event(w,
762 NULL, SND_SOC_DAPM_POST_PMU);
763 if (ret < 0)
Mark Brown81628102009-06-07 13:21:24 +0100764 pr_err("%s: post event failed: %d\n",
765 w->name, ret);
Mark Brown42aa3412009-03-01 19:21:10 +0000766 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100767
Mark Brown81628102009-06-07 13:21:24 +0100768 /* power down post event */
769 if (!w->power && w->event &&
770 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
771 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
772 w->name);
773 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
774 if (ret < 0)
775 pr_err("%s: post event failed: %d\n",
776 w->name, ret);
777 }
Mark Brown42aa3412009-03-01 19:21:10 +0000778 }
Mark Brown42aa3412009-03-01 19:21:10 +0000779}
780
Mark Brownb22ead22009-06-07 12:51:26 +0100781/* Apply a DAPM power sequence.
782 *
783 * We walk over a pre-sorted list of widgets to apply power to. In
784 * order to minimise the number of writes to the device required
785 * multiple widgets will be updated in a single write where possible.
786 * Currently anything that requires more than a single write is not
787 * handled.
788 */
789static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
790 int event, int sort[])
791{
792 struct snd_soc_dapm_widget *w, *n;
793 LIST_HEAD(pending);
794 int cur_sort = -1;
795 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100796 int ret;
797
Mark Brownb22ead22009-06-07 12:51:26 +0100798 list_for_each_entry_safe(w, n, list, power_list) {
799 ret = 0;
800
801 /* Do we need to apply any queued changes? */
802 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
803 if (!list_empty(&pending))
804 dapm_seq_run_coalesced(codec, &pending);
805
806 INIT_LIST_HEAD(&pending);
807 cur_sort = -1;
808 cur_reg = SND_SOC_NOPM;
809 }
810
Mark Brown163cac02009-06-07 10:12:52 +0100811 switch (w->id) {
812 case snd_soc_dapm_pre:
813 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100814 list_for_each_entry_safe_continue(w, n, list,
815 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100816
Mark Brownb22ead22009-06-07 12:51:26 +0100817 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100818 ret = w->event(w,
819 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100820 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100821 ret = w->event(w,
822 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100823 break;
824
825 case snd_soc_dapm_post:
826 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100827 list_for_each_entry_safe_continue(w, n, list,
828 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100829
Mark Brownb22ead22009-06-07 12:51:26 +0100830 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100831 ret = w->event(w,
832 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100833 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100834 ret = w->event(w,
835 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100836 break;
837
838 case snd_soc_dapm_input:
839 case snd_soc_dapm_output:
840 case snd_soc_dapm_hp:
841 case snd_soc_dapm_mic:
842 case snd_soc_dapm_line:
843 case snd_soc_dapm_spk:
844 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100845 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100846 break;
847
848 default:
Mark Brown81628102009-06-07 13:21:24 +0100849 /* Queue it up for application */
850 cur_sort = sort[w->id];
851 cur_reg = w->reg;
852 list_move(&w->power_list, &pending);
853 break;
Mark Brown163cac02009-06-07 10:12:52 +0100854 }
Mark Brownb22ead22009-06-07 12:51:26 +0100855
856 if (ret < 0)
857 pr_err("Failed to apply widget power: %d\n",
858 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100859 }
Mark Brownb22ead22009-06-07 12:51:26 +0100860
861 if (!list_empty(&pending))
862 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100863}
864
Mark Brown42aa3412009-03-01 19:21:10 +0000865/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200866 * Scan each dapm widget for complete audio path.
867 * A complete path is a route that has valid endpoints i.e.:-
868 *
869 * o DAC to output pin.
870 * o Input Pin to ADC.
871 * o Input pin to Output pin (bypass, sidetone)
872 * o DAC to ADC (loopback).
873 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100874static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200875{
Mark Brown452c5ea2009-05-17 21:41:23 +0100876 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200877 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100878 LIST_HEAD(up_list);
879 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100880 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100881 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100882 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883
Mark Brown6d3ddc82009-05-16 17:47:29 +0100884 /* Check which widgets we need to power and store them in
885 * lists indicating if they should be powered up or down.
886 */
887 list_for_each_entry(w, &codec->dapm_widgets, list) {
888 switch (w->id) {
889 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100890 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100891 break;
892 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100893 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100894 break;
895
896 default:
897 if (!w->power_check)
898 continue;
899
Mark Brown50b6bce2009-11-23 13:11:53 +0000900 /* If we're suspending then pull down all the
901 * power. */
902 switch (event) {
903 case SND_SOC_DAPM_STREAM_SUSPEND:
904 power = 0;
905 break;
906
907 default:
Mark Brownda341832010-03-15 19:23:37 +0000908 if (!w->force)
909 power = w->power_check(w);
910 else
911 power = 1;
Mark Brown50b6bce2009-11-23 13:11:53 +0000912 if (power)
913 sys_power = 1;
914 break;
915 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100916
Mark Brown6d3ddc82009-05-16 17:47:29 +0100917 if (w->power == power)
918 continue;
919
920 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100921 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100922 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100923 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100924
925 w->power = power;
926 break;
927 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200928 }
929
Mark Brownb14b76a2009-08-17 11:55:38 +0100930 /* If there are no DAPM widgets then try to figure out power from the
931 * event type.
932 */
933 if (list_empty(&codec->dapm_widgets)) {
934 switch (event) {
935 case SND_SOC_DAPM_STREAM_START:
936 case SND_SOC_DAPM_STREAM_RESUME:
937 sys_power = 1;
938 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000939 case SND_SOC_DAPM_STREAM_SUSPEND:
940 sys_power = 0;
941 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100942 case SND_SOC_DAPM_STREAM_NOP:
Mark Browna96ca332010-01-19 22:49:43 +0000943 switch (codec->bias_level) {
944 case SND_SOC_BIAS_STANDBY:
945 case SND_SOC_BIAS_OFF:
946 sys_power = 0;
947 break;
948 default:
949 sys_power = 1;
950 break;
951 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000952 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100953 default:
954 break;
955 }
956 }
957
Mark Browna96ca332010-01-19 22:49:43 +0000958 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
959 ret = snd_soc_dapm_set_bias_level(socdev,
960 SND_SOC_BIAS_STANDBY);
961 if (ret != 0)
962 pr_err("Failed to turn on bias: %d\n", ret);
963 }
964
Mark Brown452c5ea2009-05-17 21:41:23 +0100965 /* If we're changing to all on or all off then prepare */
966 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
967 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
968 ret = snd_soc_dapm_set_bias_level(socdev,
969 SND_SOC_BIAS_PREPARE);
970 if (ret != 0)
971 pr_err("Failed to prepare bias: %d\n", ret);
972 }
973
Mark Brown6d3ddc82009-05-16 17:47:29 +0100974 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100975 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100976
977 /* Now power up. */
Mark Brown291f3bb2009-06-07 13:57:17 +0100978 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200979
Mark Brown452c5ea2009-05-17 21:41:23 +0100980 /* If we just powered the last thing off drop to standby bias */
981 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
982 ret = snd_soc_dapm_set_bias_level(socdev,
983 SND_SOC_BIAS_STANDBY);
984 if (ret != 0)
985 pr_err("Failed to apply standby bias: %d\n", ret);
986 }
987
Mark Browna96ca332010-01-19 22:49:43 +0000988 /* If we're in standby and can support bias off then do that */
989 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
990 codec->idle_bias_off) {
991 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
992 if (ret != 0)
993 pr_err("Failed to turn off bias: %d\n", ret);
994 }
995
Mark Brown452c5ea2009-05-17 21:41:23 +0100996 /* If we just powered up then move to active bias */
997 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
998 ret = snd_soc_dapm_set_bias_level(socdev,
999 SND_SOC_BIAS_ON);
1000 if (ret != 0)
1001 pr_err("Failed to apply active bias: %d\n", ret);
1002 }
1003
Mark Browncb507e72009-07-08 18:54:57 +01001004 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1005 codec->pop_time);
Mark Brown69224712010-03-03 14:57:09 +00001006 pop_wait(codec->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001007
Mark Brown42aa3412009-03-01 19:21:10 +00001008 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001009}
1010
Mark Brown79fb9382009-08-21 16:38:13 +01001011#ifdef CONFIG_DEBUG_FS
1012static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1013{
1014 file->private_data = inode->i_private;
1015 return 0;
1016}
1017
1018static ssize_t dapm_widget_power_read_file(struct file *file,
1019 char __user *user_buf,
1020 size_t count, loff_t *ppos)
1021{
1022 struct snd_soc_dapm_widget *w = file->private_data;
1023 char *buf;
1024 int in, out;
1025 ssize_t ret;
1026 struct snd_soc_dapm_path *p = NULL;
1027
1028 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1029 if (!buf)
1030 return -ENOMEM;
1031
1032 in = is_connected_input_ep(w);
1033 dapm_clear_walk(w->codec);
1034 out = is_connected_output_ep(w);
1035 dapm_clear_walk(w->codec);
1036
Mark Brownd033c362009-12-04 15:25:56 +00001037 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001038 w->name, w->power ? "On" : "Off", in, out);
1039
Mark Brownd033c362009-12-04 15:25:56 +00001040 if (w->reg >= 0)
1041 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1042 " - R%d(0x%x) bit %d",
1043 w->reg, w->reg, w->shift);
1044
1045 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1046
Mark Brown3eef08b2009-09-14 16:49:00 +01001047 if (w->sname)
1048 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1049 w->sname,
1050 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001051
1052 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001053 if (p->connected && !p->connected(w, p->sink))
1054 continue;
1055
Mark Brown79fb9382009-08-21 16:38:13 +01001056 if (p->connect)
1057 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1058 " in %s %s\n",
1059 p->name ? p->name : "static",
1060 p->source->name);
1061 }
1062 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001063 if (p->connected && !p->connected(w, p->sink))
1064 continue;
1065
Mark Brown79fb9382009-08-21 16:38:13 +01001066 if (p->connect)
1067 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1068 " out %s %s\n",
1069 p->name ? p->name : "static",
1070 p->sink->name);
1071 }
1072
1073 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1074
1075 kfree(buf);
1076 return ret;
1077}
1078
1079static const struct file_operations dapm_widget_power_fops = {
1080 .open = dapm_widget_power_open_file,
1081 .read = dapm_widget_power_read_file,
1082};
1083
1084void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1085{
1086 struct snd_soc_dapm_widget *w;
1087 struct dentry *d;
1088
1089 if (!codec->debugfs_dapm)
1090 return;
1091
1092 list_for_each_entry(w, &codec->dapm_widgets, list) {
1093 if (!w->name)
1094 continue;
1095
1096 d = debugfs_create_file(w->name, 0444,
1097 codec->debugfs_dapm, w,
1098 &dapm_widget_power_fops);
1099 if (!d)
1100 printk(KERN_WARNING
1101 "ASoC: Failed to create %s debugfs file\n",
1102 w->name);
1103 }
1104}
1105#else
1106void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1107{
1108}
1109#endif
1110
Richard Purdie2b97eab2006-10-06 18:32:18 +02001111/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001112static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001113 struct snd_kcontrol *kcontrol, int change,
1114 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001115{
1116 struct snd_soc_dapm_path *path;
1117 int found = 0;
1118
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001119 if (widget->id != snd_soc_dapm_mux &&
1120 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001121 return -ENODEV;
1122
Mark Brown3a655772009-10-05 17:23:30 +01001123 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001124 return 0;
1125
1126 /* find dapm widget path assoc with kcontrol */
1127 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1128 if (path->kcontrol != kcontrol)
1129 continue;
1130
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001131 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132 continue;
1133
1134 found = 1;
1135 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001136 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001137 path->connect = 1; /* new connection */
1138 else
1139 path->connect = 0; /* old connection must be powered down */
1140 }
1141
Mark Brownb91b8fa2010-01-20 18:18:35 +00001142 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001143 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1144
1145 return 0;
1146}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147
Milan plzik1b075e32008-01-10 14:39:46 +01001148/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001149static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001150 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001151{
1152 struct snd_soc_dapm_path *path;
1153 int found = 0;
1154
Milan plzik1b075e32008-01-10 14:39:46 +01001155 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001156 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001157 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001158 return -ENODEV;
1159
Richard Purdie2b97eab2006-10-06 18:32:18 +02001160 /* find dapm widget path assoc with kcontrol */
1161 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1162 if (path->kcontrol != kcontrol)
1163 continue;
1164
1165 /* found, now check type */
1166 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001167 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001168 break;
1169 }
1170
Mark Brownb91b8fa2010-01-20 18:18:35 +00001171 if (found)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1173
1174 return 0;
1175}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001176
1177/* show dapm widget status in sys fs */
1178static ssize_t dapm_widget_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001182 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 struct snd_soc_dapm_widget *w;
1184 int count = 0;
1185 char *state = "not set";
1186
1187 list_for_each_entry(w, &codec->dapm_widgets, list) {
1188
1189 /* only display widgets that burnm power */
1190 switch (w->id) {
1191 case snd_soc_dapm_hp:
1192 case snd_soc_dapm_mic:
1193 case snd_soc_dapm_spk:
1194 case snd_soc_dapm_line:
1195 case snd_soc_dapm_micbias:
1196 case snd_soc_dapm_dac:
1197 case snd_soc_dapm_adc:
1198 case snd_soc_dapm_pga:
1199 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001200 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001201 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001202 if (w->name)
1203 count += sprintf(buf + count, "%s: %s\n",
1204 w->name, w->power ? "On":"Off");
1205 break;
1206 default:
1207 break;
1208 }
1209 }
1210
Mark Brown0be98982008-05-19 12:31:28 +02001211 switch (codec->bias_level) {
1212 case SND_SOC_BIAS_ON:
1213 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214 break;
Mark Brown0be98982008-05-19 12:31:28 +02001215 case SND_SOC_BIAS_PREPARE:
1216 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 break;
Mark Brown0be98982008-05-19 12:31:28 +02001218 case SND_SOC_BIAS_STANDBY:
1219 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001220 break;
Mark Brown0be98982008-05-19 12:31:28 +02001221 case SND_SOC_BIAS_OFF:
1222 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 break;
1224 }
1225 count += sprintf(buf + count, "PM State: %s\n", state);
1226
1227 return count;
1228}
1229
1230static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1231
1232int snd_soc_dapm_sys_add(struct device *dev)
1233{
Troy Kisky12ef1932008-10-13 17:42:14 -07001234 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001235}
1236
1237static void snd_soc_dapm_sys_remove(struct device *dev)
1238{
Mark Brownaef90842009-05-16 17:53:16 +01001239 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001240}
1241
1242/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001243static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001244{
1245 struct snd_soc_dapm_widget *w, *next_w;
1246 struct snd_soc_dapm_path *p, *next_p;
1247
1248 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1249 list_del(&w->list);
1250 kfree(w);
1251 }
1252
1253 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1254 list_del(&p->list);
1255 kfree(p->long_name);
1256 kfree(p);
1257 }
1258}
1259
Liam Girdwooda5302182008-07-07 13:35:17 +01001260static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001261 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001262{
1263 struct snd_soc_dapm_widget *w;
1264
1265 list_for_each_entry(w, &codec->dapm_widgets, list) {
1266 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001267 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001268 w->connected = status;
1269 return 0;
1270 }
1271 }
1272
Mark Brownc1286b82008-07-07 19:26:03 +01001273 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001274 return -EINVAL;
1275}
1276
Richard Purdie2b97eab2006-10-06 18:32:18 +02001277/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001278 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001279 * @codec: audio codec
1280 *
1281 * Walks all dapm audio paths and powers widgets according to their
1282 * stream or path usage.
1283 *
1284 * Returns 0 for success.
1285 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001286int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287{
Mark Brownb91b8fa2010-01-20 18:18:35 +00001288 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289}
Liam Girdwooda5302182008-07-07 13:35:17 +01001290EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001291
Mark Brown105f1c22008-05-13 14:52:19 +02001292static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
Mark Brown215edda2009-09-08 18:59:05 +01001293 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294{
1295 struct snd_soc_dapm_path *path;
1296 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Mark Brown215edda2009-09-08 18:59:05 +01001297 const char *sink = route->sink;
1298 const char *control = route->control;
1299 const char *source = route->source;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001300 int ret = 0;
1301
1302 /* find src and dest widgets */
1303 list_for_each_entry(w, &codec->dapm_widgets, list) {
1304
1305 if (!wsink && !(strcmp(w->name, sink))) {
1306 wsink = w;
1307 continue;
1308 }
1309 if (!wsource && !(strcmp(w->name, source))) {
1310 wsource = w;
1311 }
1312 }
1313
1314 if (wsource == NULL || wsink == NULL)
1315 return -ENODEV;
1316
1317 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1318 if (!path)
1319 return -ENOMEM;
1320
1321 path->source = wsource;
1322 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001323 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001324 INIT_LIST_HEAD(&path->list);
1325 INIT_LIST_HEAD(&path->list_source);
1326 INIT_LIST_HEAD(&path->list_sink);
1327
1328 /* check for external widgets */
1329 if (wsink->id == snd_soc_dapm_input) {
1330 if (wsource->id == snd_soc_dapm_micbias ||
1331 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001332 wsource->id == snd_soc_dapm_line ||
1333 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 wsink->ext = 1;
1335 }
1336 if (wsource->id == snd_soc_dapm_output) {
1337 if (wsink->id == snd_soc_dapm_spk ||
1338 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001339 wsink->id == snd_soc_dapm_line ||
1340 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001341 wsource->ext = 1;
1342 }
1343
1344 /* connect static paths */
1345 if (control == NULL) {
1346 list_add(&path->list, &codec->dapm_paths);
1347 list_add(&path->list_sink, &wsink->sources);
1348 list_add(&path->list_source, &wsource->sinks);
1349 path->connect = 1;
1350 return 0;
1351 }
1352
1353 /* connect dynamic paths */
1354 switch(wsink->id) {
1355 case snd_soc_dapm_adc:
1356 case snd_soc_dapm_dac:
1357 case snd_soc_dapm_pga:
1358 case snd_soc_dapm_input:
1359 case snd_soc_dapm_output:
1360 case snd_soc_dapm_micbias:
1361 case snd_soc_dapm_vmid:
1362 case snd_soc_dapm_pre:
1363 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001364 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001365 case snd_soc_dapm_aif_in:
1366 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001367 list_add(&path->list, &codec->dapm_paths);
1368 list_add(&path->list_sink, &wsink->sources);
1369 list_add(&path->list_source, &wsource->sinks);
1370 path->connect = 1;
1371 return 0;
1372 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001373 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001374 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1375 &wsink->kcontrols[0]);
1376 if (ret != 0)
1377 goto err;
1378 break;
1379 case snd_soc_dapm_switch:
1380 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001381 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001382 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1383 if (ret != 0)
1384 goto err;
1385 break;
1386 case snd_soc_dapm_hp:
1387 case snd_soc_dapm_mic:
1388 case snd_soc_dapm_line:
1389 case snd_soc_dapm_spk:
1390 list_add(&path->list, &codec->dapm_paths);
1391 list_add(&path->list_sink, &wsink->sources);
1392 list_add(&path->list_source, &wsource->sinks);
1393 path->connect = 0;
1394 return 0;
1395 }
1396 return 0;
1397
1398err:
1399 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1400 control, sink);
1401 kfree(path);
1402 return ret;
1403}
Mark Brown105f1c22008-05-13 14:52:19 +02001404
1405/**
Mark Brown105f1c22008-05-13 14:52:19 +02001406 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1407 * @codec: codec
1408 * @route: audio routes
1409 * @num: number of routes
1410 *
1411 * Connects 2 dapm widgets together via a named audio path. The sink is
1412 * the widget receiving the audio signal, whilst the source is the sender
1413 * of the audio signal.
1414 *
1415 * Returns 0 for success else error. On error all resources can be freed
1416 * with a call to snd_soc_card_free().
1417 */
1418int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1419 const struct snd_soc_dapm_route *route, int num)
1420{
1421 int i, ret;
1422
1423 for (i = 0; i < num; i++) {
Mark Brown215edda2009-09-08 18:59:05 +01001424 ret = snd_soc_dapm_add_route(codec, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001425 if (ret < 0) {
1426 printk(KERN_ERR "Failed to add route %s->%s\n",
1427 route->source,
1428 route->sink);
1429 return ret;
1430 }
1431 route++;
1432 }
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1437
1438/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439 * snd_soc_dapm_new_widgets - add new dapm widgets
1440 * @codec: audio codec
1441 *
1442 * Checks the codec for any new dapm widgets and creates them if found.
1443 *
1444 * Returns 0 for success.
1445 */
1446int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1447{
1448 struct snd_soc_dapm_widget *w;
1449
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 list_for_each_entry(w, &codec->dapm_widgets, list)
1451 {
1452 if (w->new)
1453 continue;
1454
1455 switch(w->id) {
1456 case snd_soc_dapm_switch:
1457 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001458 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001459 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001460 dapm_new_mixer(codec, w);
1461 break;
1462 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001463 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001464 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001465 dapm_new_mux(codec, w);
1466 break;
1467 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001468 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001469 w->power_check = dapm_adc_check_power;
1470 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001472 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001473 w->power_check = dapm_dac_check_power;
1474 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475 case snd_soc_dapm_pga:
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_pga(codec, w);
1478 break;
1479 case snd_soc_dapm_input:
1480 case snd_soc_dapm_output:
1481 case snd_soc_dapm_micbias:
1482 case snd_soc_dapm_spk:
1483 case snd_soc_dapm_hp:
1484 case snd_soc_dapm_mic:
1485 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001486 w->power_check = dapm_generic_check_power;
1487 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001488 case snd_soc_dapm_supply:
1489 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001490 case snd_soc_dapm_vmid:
1491 case snd_soc_dapm_pre:
1492 case snd_soc_dapm_post:
1493 break;
1494 }
1495 w->new = 1;
1496 }
1497
1498 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001499 return 0;
1500}
1501EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1502
1503/**
1504 * snd_soc_dapm_get_volsw - dapm mixer get callback
1505 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001506 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001507 *
1508 * Callback to get the value of a dapm mixer control.
1509 *
1510 * Returns 0 for success.
1511 */
1512int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1514{
1515 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001516 struct soc_mixer_control *mc =
1517 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001518 unsigned int reg = mc->reg;
1519 unsigned int shift = mc->shift;
1520 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001521 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001522 unsigned int invert = mc->invert;
1523 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001524
Richard Purdie2b97eab2006-10-06 18:32:18 +02001525 ucontrol->value.integer.value[0] =
1526 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1527 if (shift != rshift)
1528 ucontrol->value.integer.value[1] =
1529 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1530 if (invert) {
1531 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001532 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001533 if (shift != rshift)
1534 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001535 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536 }
1537
1538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1541
1542/**
1543 * snd_soc_dapm_put_volsw - dapm mixer set callback
1544 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001545 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546 *
1547 * Callback to set the value of a dapm mixer control.
1548 *
1549 * Returns 0 for success.
1550 */
1551int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_value *ucontrol)
1553{
1554 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001555 struct soc_mixer_control *mc =
1556 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001557 unsigned int reg = mc->reg;
1558 unsigned int shift = mc->shift;
1559 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001560 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001561 unsigned int mask = (1 << fls(max)) - 1;
1562 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001563 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001564 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 int ret;
1566
1567 val = (ucontrol->value.integer.value[0] & mask);
1568
1569 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001570 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 val_mask = mask << shift;
1572 val = val << shift;
1573 if (shift != rshift) {
1574 val2 = (ucontrol->value.integer.value[1] & mask);
1575 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001576 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577 val_mask |= mask << rshift;
1578 val |= val2 << rshift;
1579 }
1580
1581 mutex_lock(&widget->codec->mutex);
1582 widget->value = val;
1583
Mark Brown283375c2009-12-07 18:09:03 +00001584 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1585 if (val)
1586 /* new connection */
1587 connect = invert ? 0:1;
1588 else
1589 /* old connection must be powered down */
1590 connect = invert ? 1:0;
1591
1592 dapm_mixer_update_power(widget, kcontrol, connect);
1593 }
1594
Richard Purdie2b97eab2006-10-06 18:32:18 +02001595 if (widget->event) {
1596 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001597 ret = widget->event(widget, kcontrol,
1598 SND_SOC_DAPM_PRE_REG);
1599 if (ret < 0) {
1600 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001601 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001602 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001603 }
1604 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1605 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001606 ret = widget->event(widget, kcontrol,
1607 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608 } else
1609 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1610
1611out:
1612 mutex_unlock(&widget->codec->mutex);
1613 return ret;
1614}
1615EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1616
1617/**
1618 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1619 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001620 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001621 *
1622 * Callback to get the value of a dapm enumerated double mixer control.
1623 *
1624 * Returns 0 for success.
1625 */
1626int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol)
1628{
1629 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1630 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001631 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001632
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001633 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634 ;
1635 val = snd_soc_read(widget->codec, e->reg);
1636 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1637 if (e->shift_l != e->shift_r)
1638 ucontrol->value.enumerated.item[1] =
1639 (val >> e->shift_r) & (bitmask - 1);
1640
1641 return 0;
1642}
1643EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1644
1645/**
1646 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1647 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001648 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649 *
1650 * Callback to set the value of a dapm enumerated double mixer control.
1651 *
1652 * Returns 0 for success.
1653 */
1654int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1656{
1657 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1658 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001659 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001660 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661 int ret = 0;
1662
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001663 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001664 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001665 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 return -EINVAL;
1667 mux = ucontrol->value.enumerated.item[0];
1668 val = mux << e->shift_l;
1669 mask = (bitmask - 1) << e->shift_l;
1670 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001671 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672 return -EINVAL;
1673 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1674 mask |= (bitmask - 1) << e->shift_r;
1675 }
1676
1677 mutex_lock(&widget->codec->mutex);
1678 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001679 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1680 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001681
1682 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1683 ret = widget->event(widget,
1684 kcontrol, SND_SOC_DAPM_PRE_REG);
1685 if (ret < 0)
1686 goto out;
1687 }
1688
1689 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1690
1691 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1692 ret = widget->event(widget,
1693 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001694
1695out:
1696 mutex_unlock(&widget->codec->mutex);
1697 return ret;
1698}
1699EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1700
1701/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001702 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1703 * @kcontrol: mixer control
1704 * @ucontrol: control element information
1705 *
1706 * Returns 0 for success.
1707 */
1708int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol)
1710{
1711 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1712
1713 ucontrol->value.enumerated.item[0] = widget->value;
1714
1715 return 0;
1716}
1717EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1718
1719/**
1720 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1721 * @kcontrol: mixer control
1722 * @ucontrol: control element information
1723 *
1724 * Returns 0 for success.
1725 */
1726int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1728{
1729 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1730 struct soc_enum *e =
1731 (struct soc_enum *)kcontrol->private_value;
1732 int change;
1733 int ret = 0;
1734
1735 if (ucontrol->value.enumerated.item[0] >= e->max)
1736 return -EINVAL;
1737
1738 mutex_lock(&widget->codec->mutex);
1739
1740 change = widget->value != ucontrol->value.enumerated.item[0];
1741 widget->value = ucontrol->value.enumerated.item[0];
1742 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1743
1744 mutex_unlock(&widget->codec->mutex);
1745 return ret;
1746}
1747EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1748
1749/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001750 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1751 * callback
1752 * @kcontrol: mixer control
1753 * @ucontrol: control element information
1754 *
1755 * Callback to get the value of a dapm semi enumerated double mixer control.
1756 *
1757 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1758 * used for handling bitfield coded enumeration for example.
1759 *
1760 * Returns 0 for success.
1761 */
1762int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
1764{
1765 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001766 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001767 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001768
1769 reg_val = snd_soc_read(widget->codec, e->reg);
1770 val = (reg_val >> e->shift_l) & e->mask;
1771 for (mux = 0; mux < e->max; mux++) {
1772 if (val == e->values[mux])
1773 break;
1774 }
1775 ucontrol->value.enumerated.item[0] = mux;
1776 if (e->shift_l != e->shift_r) {
1777 val = (reg_val >> e->shift_r) & e->mask;
1778 for (mux = 0; mux < e->max; mux++) {
1779 if (val == e->values[mux])
1780 break;
1781 }
1782 ucontrol->value.enumerated.item[1] = mux;
1783 }
1784
1785 return 0;
1786}
1787EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1788
1789/**
1790 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1791 * callback
1792 * @kcontrol: mixer control
1793 * @ucontrol: control element information
1794 *
1795 * Callback to set the value of a dapm semi enumerated double mixer control.
1796 *
1797 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1798 * used for handling bitfield coded enumeration for example.
1799 *
1800 * Returns 0 for success.
1801 */
1802int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1803 struct snd_ctl_elem_value *ucontrol)
1804{
1805 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001806 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001807 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001808 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001809 int ret = 0;
1810
1811 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1812 return -EINVAL;
1813 mux = ucontrol->value.enumerated.item[0];
1814 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1815 mask = e->mask << e->shift_l;
1816 if (e->shift_l != e->shift_r) {
1817 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1818 return -EINVAL;
1819 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1820 mask |= e->mask << e->shift_r;
1821 }
1822
1823 mutex_lock(&widget->codec->mutex);
1824 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001825 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1826 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001827
1828 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1829 ret = widget->event(widget,
1830 kcontrol, SND_SOC_DAPM_PRE_REG);
1831 if (ret < 0)
1832 goto out;
1833 }
1834
1835 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1836
1837 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1838 ret = widget->event(widget,
1839 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001840
1841out:
1842 mutex_unlock(&widget->codec->mutex);
1843 return ret;
1844}
1845EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1846
1847/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001848 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1849 *
1850 * @kcontrol: mixer control
1851 * @uinfo: control element information
1852 *
1853 * Callback to provide information about a pin switch control.
1854 */
1855int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_info *uinfo)
1857{
1858 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1859 uinfo->count = 1;
1860 uinfo->value.integer.min = 0;
1861 uinfo->value.integer.max = 1;
1862
1863 return 0;
1864}
1865EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1866
1867/**
1868 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1869 *
1870 * @kcontrol: mixer control
1871 * @ucontrol: Value
1872 */
1873int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
1875{
1876 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1877 const char *pin = (const char *)kcontrol->private_value;
1878
1879 mutex_lock(&codec->mutex);
1880
1881 ucontrol->value.integer.value[0] =
1882 snd_soc_dapm_get_pin_status(codec, pin);
1883
1884 mutex_unlock(&codec->mutex);
1885
1886 return 0;
1887}
1888EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1889
1890/**
1891 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1892 *
1893 * @kcontrol: mixer control
1894 * @ucontrol: Value
1895 */
1896int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1897 struct snd_ctl_elem_value *ucontrol)
1898{
1899 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1900 const char *pin = (const char *)kcontrol->private_value;
1901
1902 mutex_lock(&codec->mutex);
1903
1904 if (ucontrol->value.integer.value[0])
1905 snd_soc_dapm_enable_pin(codec, pin);
1906 else
1907 snd_soc_dapm_disable_pin(codec, pin);
1908
1909 snd_soc_dapm_sync(codec);
1910
1911 mutex_unlock(&codec->mutex);
1912
1913 return 0;
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1916
1917/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001918 * snd_soc_dapm_new_control - create new dapm control
1919 * @codec: audio codec
1920 * @widget: widget template
1921 *
1922 * Creates a new dapm control based upon the template.
1923 *
1924 * Returns 0 for success else error.
1925 */
1926int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1927 const struct snd_soc_dapm_widget *widget)
1928{
1929 struct snd_soc_dapm_widget *w;
1930
1931 if ((w = dapm_cnew_widget(widget)) == NULL)
1932 return -ENOMEM;
1933
1934 w->codec = codec;
1935 INIT_LIST_HEAD(&w->sources);
1936 INIT_LIST_HEAD(&w->sinks);
1937 INIT_LIST_HEAD(&w->list);
1938 list_add(&w->list, &codec->dapm_widgets);
1939
1940 /* machine layer set ups unconnected pins and insertions */
1941 w->connected = 1;
1942 return 0;
1943}
1944EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1945
1946/**
Mark Brown4ba13272008-05-13 14:51:19 +02001947 * snd_soc_dapm_new_controls - create new dapm controls
1948 * @codec: audio codec
1949 * @widget: widget array
1950 * @num: number of widgets
1951 *
1952 * Creates new DAPM controls based upon the templates.
1953 *
1954 * Returns 0 for success else error.
1955 */
1956int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1957 const struct snd_soc_dapm_widget *widget,
1958 int num)
1959{
1960 int i, ret;
1961
1962 for (i = 0; i < num; i++) {
1963 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001964 if (ret < 0) {
1965 printk(KERN_ERR
1966 "ASoC: Failed to create DAPM control %s: %d\n",
1967 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001968 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001969 }
Mark Brown4ba13272008-05-13 14:51:19 +02001970 widget++;
1971 }
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1975
1976
1977/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1979 * @codec: audio codec
1980 * @stream: stream name
1981 * @event: stream event
1982 *
1983 * Sends a stream event to the dapm core. The core then makes any
1984 * necessary widget power changes.
1985 *
1986 * Returns 0 for success else error.
1987 */
1988int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1989 char *stream, int event)
1990{
1991 struct snd_soc_dapm_widget *w;
1992
Seth Forshee11da21a2007-02-02 17:14:19 +01001993 if (stream == NULL)
1994 return 0;
1995
Richard Purdie2b97eab2006-10-06 18:32:18 +02001996 mutex_lock(&codec->mutex);
1997 list_for_each_entry(w, &codec->dapm_widgets, list)
1998 {
1999 if (!w->sname)
2000 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01002001 pr_debug("widget %s\n %s stream %s event %d\n",
2002 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 if (strstr(w->sname, stream)) {
2004 switch(event) {
2005 case SND_SOC_DAPM_STREAM_START:
2006 w->active = 1;
2007 break;
2008 case SND_SOC_DAPM_STREAM_STOP:
2009 w->active = 0;
2010 break;
2011 case SND_SOC_DAPM_STREAM_SUSPEND:
2012 if (w->active)
2013 w->suspend = 1;
2014 w->active = 0;
2015 break;
2016 case SND_SOC_DAPM_STREAM_RESUME:
2017 if (w->suspend) {
2018 w->active = 1;
2019 w->suspend = 0;
2020 }
2021 break;
2022 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2023 break;
2024 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");