blob: f4a8753c84c05888f4f07c1e19bdbca6b700d994 [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
15 * DAC's/ADC's.
16 * 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>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc-dapm.h>
44#include <sound/initval.h>
45
46/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010047#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#else
50#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#endif
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
55 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +020056 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000057 snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga,
58 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020059};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000060
Richard Purdie2b97eab2006-10-06 18:32:18 +020061static int dapm_down_seq[] = {
62 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000063 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
64 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
65 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020066};
67
68static int dapm_status = 1;
69module_param(dapm_status, int, 0);
70MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
71
Troy Kisky12ef1932008-10-13 17:42:14 -070072static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010073{
74 if (pop_time)
75 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
76}
77
Troy Kisky12ef1932008-10-13 17:42:14 -070078static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010079{
80 va_list args;
81
82 va_start(args, fmt);
83
84 if (pop_time) {
85 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070086 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010087 }
88
89 va_end(args);
90}
91
Richard Purdie2b97eab2006-10-06 18:32:18 +020092/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010093static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020094 const struct snd_soc_dapm_widget *_widget)
95{
Takashi Iwai88cb4292007-02-05 14:56:20 +010096 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020097}
98
99/* set up initial codec paths */
100static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
101 struct snd_soc_dapm_path *p, int i)
102{
103 switch (w->id) {
104 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000105 case snd_soc_dapm_mixer:
106 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100108 struct soc_mixer_control *mc = (struct soc_mixer_control *)
109 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400110 unsigned int reg = mc->reg;
111 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100112 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400113 unsigned int mask = (1 << fls(max)) - 1;
114 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200115
116 val = snd_soc_read(w->codec, reg);
117 val = (val >> shift) & mask;
118
119 if ((invert && !val) || (!invert && val))
120 p->connect = 1;
121 else
122 p->connect = 0;
123 }
124 break;
125 case snd_soc_dapm_mux: {
126 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
127 int val, item, bitmask;
128
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100129 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200130 ;
131 val = snd_soc_read(w->codec, e->reg);
132 item = (val >> e->shift_l) & (bitmask - 1);
133
134 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100135 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200136 if (!(strcmp(p->name, e->texts[i])) && item == i)
137 p->connect = 1;
138 }
139 }
140 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200141 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200142 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200143 w->kcontrols[i].private_value;
144 int val, item;
145
146 val = snd_soc_read(w->codec, e->reg);
147 val = (val >> e->shift_l) & e->mask;
148 for (item = 0; item < e->max; item++) {
149 if (val == e->values[item])
150 break;
151 }
152
153 p->connect = 0;
154 for (i = 0; i < e->max; i++) {
155 if (!(strcmp(p->name, e->texts[i])) && item == i)
156 p->connect = 1;
157 }
158 }
159 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200160 /* does not effect routing - always connected */
161 case snd_soc_dapm_pga:
162 case snd_soc_dapm_output:
163 case snd_soc_dapm_adc:
164 case snd_soc_dapm_input:
165 case snd_soc_dapm_dac:
166 case snd_soc_dapm_micbias:
167 case snd_soc_dapm_vmid:
168 p->connect = 1;
169 break;
170 /* does effect routing - dynamically connected */
171 case snd_soc_dapm_hp:
172 case snd_soc_dapm_mic:
173 case snd_soc_dapm_spk:
174 case snd_soc_dapm_line:
175 case snd_soc_dapm_pre:
176 case snd_soc_dapm_post:
177 p->connect = 0;
178 break;
179 }
180}
181
182/* connect mux widget to it's interconnecting audio paths */
183static int dapm_connect_mux(struct snd_soc_codec *codec,
184 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
185 struct snd_soc_dapm_path *path, const char *control_name,
186 const struct snd_kcontrol_new *kcontrol)
187{
188 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
189 int i;
190
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100191 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200192 if (!(strcmp(control_name, e->texts[i]))) {
193 list_add(&path->list, &codec->dapm_paths);
194 list_add(&path->list_sink, &dest->sources);
195 list_add(&path->list_source, &src->sinks);
196 path->name = (char*)e->texts[i];
197 dapm_set_path_status(dest, path, 0);
198 return 0;
199 }
200 }
201
202 return -ENODEV;
203}
204
205/* connect mixer widget to it's interconnecting audio paths */
206static int dapm_connect_mixer(struct snd_soc_codec *codec,
207 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
208 struct snd_soc_dapm_path *path, const char *control_name)
209{
210 int i;
211
212 /* search for mixer kcontrol */
213 for (i = 0; i < dest->num_kcontrols; i++) {
214 if (!strcmp(control_name, dest->kcontrols[i].name)) {
215 list_add(&path->list, &codec->dapm_paths);
216 list_add(&path->list_sink, &dest->sources);
217 list_add(&path->list_source, &src->sinks);
218 path->name = dest->kcontrols[i].name;
219 dapm_set_path_status(dest, path, i);
220 return 0;
221 }
222 }
223 return -ENODEV;
224}
225
226/* update dapm codec register bits */
227static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
228{
229 int change, power;
230 unsigned short old, new;
231 struct snd_soc_codec *codec = widget->codec;
232
233 /* check for valid widgets */
234 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
235 widget->id == snd_soc_dapm_output ||
236 widget->id == snd_soc_dapm_hp ||
237 widget->id == snd_soc_dapm_mic ||
238 widget->id == snd_soc_dapm_line ||
239 widget->id == snd_soc_dapm_spk)
240 return 0;
241
242 power = widget->power;
243 if (widget->invert)
244 power = (power ? 0:1);
245
246 old = snd_soc_read(codec, widget->reg);
247 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
248
249 change = old != new;
250 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700251 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
252 widget->name, widget->power ? "on" : "off",
253 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200254 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700255 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200256 }
Mark Brownc1286b82008-07-07 19:26:03 +0100257 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
258 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200259 return change;
260}
261
262/* ramps the volume up or down to minimise pops before or after a
263 * DAPM power event */
264static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
265{
266 const struct snd_kcontrol_new *k = widget->kcontrols;
267
268 if (widget->muted && !power)
269 return 0;
270 if (!widget->muted && power)
271 return 0;
272
273 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100274 struct soc_mixer_control *mc =
275 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400276 unsigned int reg = mc->reg;
277 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100278 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400279 unsigned int mask = (1 << fls(max)) - 1;
280 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200281
282 if (power) {
283 int i;
284 /* power up has happended, increase volume to last level */
285 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100286 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200287 snd_soc_update_bits(widget->codec, reg, mask, i);
288 } else {
289 for (i = 0; i < widget->saved_value; i++)
290 snd_soc_update_bits(widget->codec, reg, mask, i);
291 }
292 widget->muted = 0;
293 } else {
294 /* power down is about to occur, decrease volume to mute */
295 int val = snd_soc_read(widget->codec, reg);
296 int i = widget->saved_value = (val >> shift) & mask;
297 if (invert) {
298 for (; i < mask; i++)
299 snd_soc_update_bits(widget->codec, reg, mask, i);
300 } else {
301 for (; i > 0; i--)
302 snd_soc_update_bits(widget->codec, reg, mask, i);
303 }
304 widget->muted = 1;
305 }
306 }
307 return 0;
308}
309
310/* create new dapm mixer control */
311static int dapm_new_mixer(struct snd_soc_codec *codec,
312 struct snd_soc_dapm_widget *w)
313{
314 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000315 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200316 struct snd_soc_dapm_path *path;
317
318 /* add kcontrol */
319 for (i = 0; i < w->num_kcontrols; i++) {
320
321 /* match name */
322 list_for_each_entry(path, &w->sources, list_sink) {
323
324 /* mixer/mux paths name must match control name */
325 if (path->name != (char*)w->kcontrols[i].name)
326 continue;
327
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000328 /* add dapm control with long name.
329 * for dapm_mixer this is the concatenation of the
330 * mixer and kcontrol name.
331 * for dapm_mixer_named_ctl this is simply the
332 * kcontrol name.
333 */
334 name_len = strlen(w->kcontrols[i].name) + 1;
335 if (w->id == snd_soc_dapm_mixer)
336 name_len += 1 + strlen(w->name);
337
Mark Brown219b93f2008-10-28 13:02:31 +0000338 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339
Richard Purdie2b97eab2006-10-06 18:32:18 +0200340 if (path->long_name == NULL)
341 return -ENOMEM;
342
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000343 switch (w->id) {
344 case snd_soc_dapm_mixer:
345 default:
346 snprintf(path->long_name, name_len, "%s %s",
347 w->name, w->kcontrols[i].name);
348 break;
349 case snd_soc_dapm_mixer_named_ctl:
350 snprintf(path->long_name, name_len, "%s",
351 w->kcontrols[i].name);
352 break;
353 }
354
Mark Brown219b93f2008-10-28 13:02:31 +0000355 path->long_name[name_len - 1] = '\0';
356
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
358 path->long_name);
359 ret = snd_ctl_add(codec->card, path->kcontrol);
360 if (ret < 0) {
361 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
362 path->long_name);
363 kfree(path->long_name);
364 path->long_name = NULL;
365 return ret;
366 }
367 }
368 }
369 return ret;
370}
371
372/* create new dapm mux control */
373static int dapm_new_mux(struct snd_soc_codec *codec,
374 struct snd_soc_dapm_widget *w)
375{
376 struct snd_soc_dapm_path *path = NULL;
377 struct snd_kcontrol *kcontrol;
378 int ret = 0;
379
380 if (!w->num_kcontrols) {
381 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
382 return -EINVAL;
383 }
384
385 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
386 ret = snd_ctl_add(codec->card, kcontrol);
387 if (ret < 0)
388 goto err;
389
390 list_for_each_entry(path, &w->sources, list_sink)
391 path->kcontrol = kcontrol;
392
393 return ret;
394
395err:
396 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
397 return ret;
398}
399
400/* create new dapm volume control */
401static int dapm_new_pga(struct snd_soc_codec *codec,
402 struct snd_soc_dapm_widget *w)
403{
404 struct snd_kcontrol *kcontrol;
405 int ret = 0;
406
407 if (!w->num_kcontrols)
408 return -EINVAL;
409
410 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
411 ret = snd_ctl_add(codec->card, kcontrol);
412 if (ret < 0) {
413 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
414 return ret;
415 }
416
417 return ret;
418}
419
420/* reset 'walked' bit for each dapm path */
421static inline void dapm_clear_walk(struct snd_soc_codec *codec)
422{
423 struct snd_soc_dapm_path *p;
424
425 list_for_each_entry(p, &codec->dapm_paths, list)
426 p->walked = 0;
427}
428
429/*
430 * Recursively check for a completed path to an active or physically connected
431 * output widget. Returns number of complete paths.
432 */
433static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
434{
435 struct snd_soc_dapm_path *path;
436 int con = 0;
437
438 if (widget->id == snd_soc_dapm_adc && widget->active)
439 return 1;
440
441 if (widget->connected) {
442 /* connected pin ? */
443 if (widget->id == snd_soc_dapm_output && !widget->ext)
444 return 1;
445
446 /* connected jack or spk ? */
447 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
448 widget->id == snd_soc_dapm_line)
449 return 1;
450 }
451
452 list_for_each_entry(path, &widget->sinks, list_source) {
453 if (path->walked)
454 continue;
455
456 if (path->sink && path->connect) {
457 path->walked = 1;
458 con += is_connected_output_ep(path->sink);
459 }
460 }
461
462 return con;
463}
464
465/*
466 * Recursively check for a completed path to an active or physically connected
467 * input widget. Returns number of complete paths.
468 */
469static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
470{
471 struct snd_soc_dapm_path *path;
472 int con = 0;
473
474 /* active stream ? */
475 if (widget->id == snd_soc_dapm_dac && widget->active)
476 return 1;
477
478 if (widget->connected) {
479 /* connected pin ? */
480 if (widget->id == snd_soc_dapm_input && !widget->ext)
481 return 1;
482
483 /* connected VMID/Bias for lower pops */
484 if (widget->id == snd_soc_dapm_vmid)
485 return 1;
486
487 /* connected jack ? */
488 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
489 return 1;
490 }
491
492 list_for_each_entry(path, &widget->sources, list_sink) {
493 if (path->walked)
494 continue;
495
496 if (path->source && path->connect) {
497 path->walked = 1;
498 con += is_connected_input_ep(path->source);
499 }
500 }
501
502 return con;
503}
504
505/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300506 * Handler for generic register modifier widget.
507 */
508int dapm_reg_event(struct snd_soc_dapm_widget *w,
509 struct snd_kcontrol *kcontrol, int event)
510{
511 unsigned int val;
512
513 if (SND_SOC_DAPM_EVENT_ON(event))
514 val = w->on_val;
515 else
516 val = w->off_val;
517
518 snd_soc_update_bits(w->codec, -(w->reg + 1),
519 w->mask << w->shift, val << w->shift);
520
521 return 0;
522}
Mark Brown11589412008-07-29 11:42:23 +0100523EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300524
525/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 * Scan each dapm widget for complete audio path.
527 * A complete path is a route that has valid endpoints i.e.:-
528 *
529 * o DAC to output pin.
530 * o Input Pin to ADC.
531 * o Input pin to Output pin (bypass, sidetone)
532 * o DAC to ADC (loopback).
533 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100534static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535{
536 struct snd_soc_dapm_widget *w;
537 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
538
539 /* do we have a sequenced stream event */
540 if (event == SND_SOC_DAPM_STREAM_START) {
541 c = ARRAY_SIZE(dapm_up_seq);
542 seq = dapm_up_seq;
543 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
544 c = ARRAY_SIZE(dapm_down_seq);
545 seq = dapm_down_seq;
546 }
547
548 for(i = 0; i < c; i++) {
549 list_for_each_entry(w, &codec->dapm_widgets, list) {
550
551 /* is widget in stream order */
552 if (seq && seq[i] && w->id != seq[i])
553 continue;
554
555 /* vmid - no action */
556 if (w->id == snd_soc_dapm_vmid)
557 continue;
558
559 /* active ADC */
560 if (w->id == snd_soc_dapm_adc && w->active) {
561 in = is_connected_input_ep(w);
562 dapm_clear_walk(w->codec);
563 w->power = (in != 0) ? 1 : 0;
564 dapm_update_bits(w);
565 continue;
566 }
567
568 /* active DAC */
569 if (w->id == snd_soc_dapm_dac && w->active) {
570 out = is_connected_output_ep(w);
571 dapm_clear_walk(w->codec);
572 w->power = (out != 0) ? 1 : 0;
573 dapm_update_bits(w);
574 continue;
575 }
576
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577 /* pre and post event widgets */
578 if (w->id == snd_soc_dapm_pre) {
579 if (!w->event)
580 continue;
581
582 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100583 ret = w->event(w,
584 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200585 if (ret < 0)
586 return ret;
587 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100588 ret = w->event(w,
589 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200590 if (ret < 0)
591 return ret;
592 }
593 continue;
594 }
595 if (w->id == snd_soc_dapm_post) {
596 if (!w->event)
597 continue;
598
599 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100600 ret = w->event(w,
601 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 if (ret < 0)
603 return ret;
604 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100605 ret = w->event(w,
606 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607 if (ret < 0)
608 return ret;
609 }
610 continue;
611 }
612
613 /* all other widgets */
614 in = is_connected_input_ep(w);
615 dapm_clear_walk(w->codec);
616 out = is_connected_output_ep(w);
617 dapm_clear_walk(w->codec);
618 power = (out != 0 && in != 0) ? 1 : 0;
619 power_change = (w->power == power) ? 0: 1;
620 w->power = power;
621
Mark Brown2927d6e2008-07-17 15:06:50 +0100622 if (!power_change)
623 continue;
624
Richard Purdie2b97eab2006-10-06 18:32:18 +0200625 /* call any power change event handlers */
Mark Brown2927d6e2008-07-17 15:06:50 +0100626 if (w->event)
627 pr_debug("power %s event for %s flags %x\n",
628 w->power ? "on" : "off",
629 w->name, w->event_flags);
630
631 /* power up pre event */
632 if (power && w->event &&
633 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
634 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
635 if (ret < 0)
636 return ret;
637 }
638
639 /* power down pre event */
640 if (!power && w->event &&
641 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
642 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
643 if (ret < 0)
644 return ret;
645 }
646
Mark Brown9dd8d812008-07-17 15:06:51 +0100647 /* Lower PGA volume to reduce pops */
648 if (w->id == snd_soc_dapm_pga && !power)
649 dapm_set_pga(w, power);
650
Mark Brown2927d6e2008-07-17 15:06:50 +0100651 dapm_update_bits(w);
652
Mark Brown9dd8d812008-07-17 15:06:51 +0100653 /* Raise PGA volume to reduce pops */
654 if (w->id == snd_soc_dapm_pga && power)
655 dapm_set_pga(w, power);
656
Mark Brown2927d6e2008-07-17 15:06:50 +0100657 /* power up post event */
658 if (power && w->event &&
659 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
660 ret = w->event(w,
661 NULL, SND_SOC_DAPM_POST_PMU);
662 if (ret < 0)
663 return ret;
664 }
665
666 /* power down post event */
667 if (!power && w->event &&
668 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
669 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
670 if (ret < 0)
671 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672 }
673 }
674 }
675
676 return ret;
677}
678
Mark Brownc1286b82008-07-07 19:26:03 +0100679#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
681{
682 struct snd_soc_dapm_widget *w;
683 struct snd_soc_dapm_path *p = NULL;
684 int in, out;
685
686 printk("DAPM %s %s\n", codec->name, action);
687
688 list_for_each_entry(w, &codec->dapm_widgets, list) {
689
690 /* only display widgets that effect routing */
691 switch (w->id) {
692 case snd_soc_dapm_pre:
693 case snd_soc_dapm_post:
694 case snd_soc_dapm_vmid:
695 continue;
696 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200697 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 case snd_soc_dapm_output:
699 case snd_soc_dapm_input:
700 case snd_soc_dapm_switch:
701 case snd_soc_dapm_hp:
702 case snd_soc_dapm_mic:
703 case snd_soc_dapm_spk:
704 case snd_soc_dapm_line:
705 case snd_soc_dapm_micbias:
706 case snd_soc_dapm_dac:
707 case snd_soc_dapm_adc:
708 case snd_soc_dapm_pga:
709 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000710 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 if (w->name) {
712 in = is_connected_input_ep(w);
713 dapm_clear_walk(w->codec);
714 out = is_connected_output_ep(w);
715 dapm_clear_walk(w->codec);
716 printk("%s: %s in %d out %d\n", w->name,
717 w->power ? "On":"Off",in, out);
718
719 list_for_each_entry(p, &w->sources, list_sink) {
720 if (p->connect)
721 printk(" in %s %s\n", p->name ? p->name : "static",
722 p->source->name);
723 }
724 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725 if (p->connect)
726 printk(" out %s %s\n", p->name ? p->name : "static",
727 p->sink->name);
728 }
729 }
730 break;
731 }
732 }
733}
734#endif
735
736/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100737static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
738 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800739 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740{
741 struct snd_soc_dapm_path *path;
742 int found = 0;
743
Peter Ujfalusieff317d2009-01-15 14:40:47 +0200744 if (widget->id != snd_soc_dapm_mux &&
745 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746 return -ENODEV;
747
748 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
749 return 0;
750
751 /* find dapm widget path assoc with kcontrol */
752 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
753 if (path->kcontrol != kcontrol)
754 continue;
755
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800756 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757 continue;
758
759 found = 1;
760 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800761 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200762 path->connect = 1; /* new connection */
763 else
764 path->connect = 0; /* old connection must be powered down */
765 }
766
Mark Brown3fccd8b2008-07-07 19:26:04 +0100767 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200768 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100769 dump_dapm(widget->codec, "mux power update");
770 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200771
772 return 0;
773}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200774
Milan plzik1b075e32008-01-10 14:39:46 +0100775/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100776static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
777 struct snd_kcontrol *kcontrol, int reg,
778 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200779{
780 struct snd_soc_dapm_path *path;
781 int found = 0;
782
Milan plzik1b075e32008-01-10 14:39:46 +0100783 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000784 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100785 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200786 return -ENODEV;
787
788 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
789 return 0;
790
791 /* find dapm widget path assoc with kcontrol */
792 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
793 if (path->kcontrol != kcontrol)
794 continue;
795
796 /* found, now check type */
797 found = 1;
798 if (val)
799 /* new connection */
800 path->connect = invert ? 0:1;
801 else
802 /* old connection must be powered down */
803 path->connect = invert ? 1:0;
804 break;
805 }
806
Mark Brown3fccd8b2008-07-07 19:26:04 +0100807 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100809 dump_dapm(widget->codec, "mixer power update");
810 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811
812 return 0;
813}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814
815/* show dapm widget status in sys fs */
816static ssize_t dapm_widget_show(struct device *dev,
817 struct device_attribute *attr, char *buf)
818{
819 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +0000820 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200821 struct snd_soc_dapm_widget *w;
822 int count = 0;
823 char *state = "not set";
824
825 list_for_each_entry(w, &codec->dapm_widgets, list) {
826
827 /* only display widgets that burnm power */
828 switch (w->id) {
829 case snd_soc_dapm_hp:
830 case snd_soc_dapm_mic:
831 case snd_soc_dapm_spk:
832 case snd_soc_dapm_line:
833 case snd_soc_dapm_micbias:
834 case snd_soc_dapm_dac:
835 case snd_soc_dapm_adc:
836 case snd_soc_dapm_pga:
837 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000838 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839 if (w->name)
840 count += sprintf(buf + count, "%s: %s\n",
841 w->name, w->power ? "On":"Off");
842 break;
843 default:
844 break;
845 }
846 }
847
Mark Brown0be98982008-05-19 12:31:28 +0200848 switch (codec->bias_level) {
849 case SND_SOC_BIAS_ON:
850 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851 break;
Mark Brown0be98982008-05-19 12:31:28 +0200852 case SND_SOC_BIAS_PREPARE:
853 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854 break;
Mark Brown0be98982008-05-19 12:31:28 +0200855 case SND_SOC_BIAS_STANDBY:
856 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857 break;
Mark Brown0be98982008-05-19 12:31:28 +0200858 case SND_SOC_BIAS_OFF:
859 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860 break;
861 }
862 count += sprintf(buf + count, "PM State: %s\n", state);
863
864 return count;
865}
866
867static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
868
869int snd_soc_dapm_sys_add(struct device *dev)
870{
Mark Brownf0062a92008-08-28 12:46:24 +0100871 if (!dapm_status)
872 return 0;
Troy Kisky12ef1932008-10-13 17:42:14 -0700873 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200874}
875
876static void snd_soc_dapm_sys_remove(struct device *dev)
877{
Mark Brown15e4c722008-07-02 11:51:20 +0100878 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200879 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100880 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200881}
882
883/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100884static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885{
886 struct snd_soc_dapm_widget *w, *next_w;
887 struct snd_soc_dapm_path *p, *next_p;
888
889 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
890 list_del(&w->list);
891 kfree(w);
892 }
893
894 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
895 list_del(&p->list);
896 kfree(p->long_name);
897 kfree(p);
898 }
899}
900
Liam Girdwooda5302182008-07-07 13:35:17 +0100901static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +0000902 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +0100903{
904 struct snd_soc_dapm_widget *w;
905
906 list_for_each_entry(w, &codec->dapm_widgets, list) {
907 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100908 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100909 w->connected = status;
910 return 0;
911 }
912 }
913
Mark Brownc1286b82008-07-07 19:26:03 +0100914 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100915 return -EINVAL;
916}
917
Richard Purdie2b97eab2006-10-06 18:32:18 +0200918/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100919 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200920 * @codec: audio codec
921 *
922 * Walks all dapm audio paths and powers widgets according to their
923 * stream or path usage.
924 *
925 * Returns 0 for success.
926 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100927int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200928{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100929 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
930 dump_dapm(codec, "sync");
931 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932}
Liam Girdwooda5302182008-07-07 13:35:17 +0100933EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934
Mark Brown105f1c22008-05-13 14:52:19 +0200935static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
936 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937{
938 struct snd_soc_dapm_path *path;
939 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
940 int ret = 0;
941
942 /* find src and dest widgets */
943 list_for_each_entry(w, &codec->dapm_widgets, list) {
944
945 if (!wsink && !(strcmp(w->name, sink))) {
946 wsink = w;
947 continue;
948 }
949 if (!wsource && !(strcmp(w->name, source))) {
950 wsource = w;
951 }
952 }
953
954 if (wsource == NULL || wsink == NULL)
955 return -ENODEV;
956
957 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
958 if (!path)
959 return -ENOMEM;
960
961 path->source = wsource;
962 path->sink = wsink;
963 INIT_LIST_HEAD(&path->list);
964 INIT_LIST_HEAD(&path->list_source);
965 INIT_LIST_HEAD(&path->list_sink);
966
967 /* check for external widgets */
968 if (wsink->id == snd_soc_dapm_input) {
969 if (wsource->id == snd_soc_dapm_micbias ||
970 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +0200971 wsink->id == snd_soc_dapm_line ||
972 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973 wsink->ext = 1;
974 }
975 if (wsource->id == snd_soc_dapm_output) {
976 if (wsink->id == snd_soc_dapm_spk ||
977 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +0200978 wsink->id == snd_soc_dapm_line ||
979 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200980 wsource->ext = 1;
981 }
982
983 /* connect static paths */
984 if (control == NULL) {
985 list_add(&path->list, &codec->dapm_paths);
986 list_add(&path->list_sink, &wsink->sources);
987 list_add(&path->list_source, &wsource->sinks);
988 path->connect = 1;
989 return 0;
990 }
991
992 /* connect dynamic paths */
993 switch(wsink->id) {
994 case snd_soc_dapm_adc:
995 case snd_soc_dapm_dac:
996 case snd_soc_dapm_pga:
997 case snd_soc_dapm_input:
998 case snd_soc_dapm_output:
999 case snd_soc_dapm_micbias:
1000 case snd_soc_dapm_vmid:
1001 case snd_soc_dapm_pre:
1002 case snd_soc_dapm_post:
1003 list_add(&path->list, &codec->dapm_paths);
1004 list_add(&path->list_sink, &wsink->sources);
1005 list_add(&path->list_source, &wsource->sinks);
1006 path->connect = 1;
1007 return 0;
1008 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001009 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1011 &wsink->kcontrols[0]);
1012 if (ret != 0)
1013 goto err;
1014 break;
1015 case snd_soc_dapm_switch:
1016 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001017 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1019 if (ret != 0)
1020 goto err;
1021 break;
1022 case snd_soc_dapm_hp:
1023 case snd_soc_dapm_mic:
1024 case snd_soc_dapm_line:
1025 case snd_soc_dapm_spk:
1026 list_add(&path->list, &codec->dapm_paths);
1027 list_add(&path->list_sink, &wsink->sources);
1028 list_add(&path->list_source, &wsource->sinks);
1029 path->connect = 0;
1030 return 0;
1031 }
1032 return 0;
1033
1034err:
1035 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1036 control, sink);
1037 kfree(path);
1038 return ret;
1039}
Mark Brown105f1c22008-05-13 14:52:19 +02001040
1041/**
Mark Brown105f1c22008-05-13 14:52:19 +02001042 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1043 * @codec: codec
1044 * @route: audio routes
1045 * @num: number of routes
1046 *
1047 * Connects 2 dapm widgets together via a named audio path. The sink is
1048 * the widget receiving the audio signal, whilst the source is the sender
1049 * of the audio signal.
1050 *
1051 * Returns 0 for success else error. On error all resources can be freed
1052 * with a call to snd_soc_card_free().
1053 */
1054int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1055 const struct snd_soc_dapm_route *route, int num)
1056{
1057 int i, ret;
1058
1059 for (i = 0; i < num; i++) {
1060 ret = snd_soc_dapm_add_route(codec, route->sink,
1061 route->control, route->source);
1062 if (ret < 0) {
1063 printk(KERN_ERR "Failed to add route %s->%s\n",
1064 route->source,
1065 route->sink);
1066 return ret;
1067 }
1068 route++;
1069 }
1070
1071 return 0;
1072}
1073EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1074
1075/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076 * snd_soc_dapm_new_widgets - add new dapm widgets
1077 * @codec: audio codec
1078 *
1079 * Checks the codec for any new dapm widgets and creates them if found.
1080 *
1081 * Returns 0 for success.
1082 */
1083int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1084{
1085 struct snd_soc_dapm_widget *w;
1086
Richard Purdie2b97eab2006-10-06 18:32:18 +02001087 list_for_each_entry(w, &codec->dapm_widgets, list)
1088 {
1089 if (w->new)
1090 continue;
1091
1092 switch(w->id) {
1093 case snd_soc_dapm_switch:
1094 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001095 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001096 dapm_new_mixer(codec, w);
1097 break;
1098 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001099 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001100 dapm_new_mux(codec, w);
1101 break;
1102 case snd_soc_dapm_adc:
1103 case snd_soc_dapm_dac:
1104 case snd_soc_dapm_pga:
1105 dapm_new_pga(codec, w);
1106 break;
1107 case snd_soc_dapm_input:
1108 case snd_soc_dapm_output:
1109 case snd_soc_dapm_micbias:
1110 case snd_soc_dapm_spk:
1111 case snd_soc_dapm_hp:
1112 case snd_soc_dapm_mic:
1113 case snd_soc_dapm_line:
1114 case snd_soc_dapm_vmid:
1115 case snd_soc_dapm_pre:
1116 case snd_soc_dapm_post:
1117 break;
1118 }
1119 w->new = 1;
1120 }
1121
1122 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001123 return 0;
1124}
1125EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1126
1127/**
1128 * snd_soc_dapm_get_volsw - dapm mixer get callback
1129 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001130 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 *
1132 * Callback to get the value of a dapm mixer control.
1133 *
1134 * Returns 0 for success.
1135 */
1136int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1137 struct snd_ctl_elem_value *ucontrol)
1138{
1139 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001140 struct soc_mixer_control *mc =
1141 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001142 unsigned int reg = mc->reg;
1143 unsigned int shift = mc->shift;
1144 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001145 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001146 unsigned int invert = mc->invert;
1147 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001148
1149 /* return the saved value if we are powered down */
1150 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1151 ucontrol->value.integer.value[0] = widget->saved_value;
1152 return 0;
1153 }
1154
1155 ucontrol->value.integer.value[0] =
1156 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1157 if (shift != rshift)
1158 ucontrol->value.integer.value[1] =
1159 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1160 if (invert) {
1161 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001162 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001163 if (shift != rshift)
1164 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001165 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001166 }
1167
1168 return 0;
1169}
1170EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1171
1172/**
1173 * snd_soc_dapm_put_volsw - dapm mixer set callback
1174 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001175 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001176 *
1177 * Callback to set the value of a dapm mixer control.
1178 *
1179 * Returns 0 for success.
1180 */
1181int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1182 struct snd_ctl_elem_value *ucontrol)
1183{
1184 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001185 struct soc_mixer_control *mc =
1186 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001187 unsigned int reg = mc->reg;
1188 unsigned int shift = mc->shift;
1189 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001190 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001191 unsigned int mask = (1 << fls(max)) - 1;
1192 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001193 unsigned short val, val2, val_mask;
1194 int ret;
1195
1196 val = (ucontrol->value.integer.value[0] & mask);
1197
1198 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001199 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001200 val_mask = mask << shift;
1201 val = val << shift;
1202 if (shift != rshift) {
1203 val2 = (ucontrol->value.integer.value[1] & mask);
1204 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001205 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206 val_mask |= mask << rshift;
1207 val |= val2 << rshift;
1208 }
1209
1210 mutex_lock(&widget->codec->mutex);
1211 widget->value = val;
1212
1213 /* save volume value if the widget is powered down */
1214 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1215 widget->saved_value = val;
1216 mutex_unlock(&widget->codec->mutex);
1217 return 1;
1218 }
1219
1220 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1221 if (widget->event) {
1222 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001223 ret = widget->event(widget, kcontrol,
1224 SND_SOC_DAPM_PRE_REG);
1225 if (ret < 0) {
1226 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001227 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001228 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229 }
1230 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1231 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001232 ret = widget->event(widget, kcontrol,
1233 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001234 } else
1235 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1236
1237out:
1238 mutex_unlock(&widget->codec->mutex);
1239 return ret;
1240}
1241EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1242
1243/**
1244 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1245 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001246 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001247 *
1248 * Callback to get the value of a dapm enumerated double mixer control.
1249 *
1250 * Returns 0 for success.
1251 */
1252int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1253 struct snd_ctl_elem_value *ucontrol)
1254{
1255 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1256 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1257 unsigned short val, bitmask;
1258
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001259 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 ;
1261 val = snd_soc_read(widget->codec, e->reg);
1262 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1263 if (e->shift_l != e->shift_r)
1264 ucontrol->value.enumerated.item[1] =
1265 (val >> e->shift_r) & (bitmask - 1);
1266
1267 return 0;
1268}
1269EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1270
1271/**
1272 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1273 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001274 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275 *
1276 * Callback to set the value of a dapm enumerated double mixer control.
1277 *
1278 * Returns 0 for success.
1279 */
1280int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1281 struct snd_ctl_elem_value *ucontrol)
1282{
1283 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1284 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1285 unsigned short val, mux;
1286 unsigned short mask, bitmask;
1287 int ret = 0;
1288
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001289 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001290 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001291 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001292 return -EINVAL;
1293 mux = ucontrol->value.enumerated.item[0];
1294 val = mux << e->shift_l;
1295 mask = (bitmask - 1) << e->shift_l;
1296 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001297 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001298 return -EINVAL;
1299 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1300 mask |= (bitmask - 1) << e->shift_r;
1301 }
1302
1303 mutex_lock(&widget->codec->mutex);
1304 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001305 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001306 if (widget->event) {
1307 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001308 ret = widget->event(widget,
1309 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001310 if (ret < 0)
1311 goto out;
1312 }
1313 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1314 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001315 ret = widget->event(widget,
1316 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001317 } else
1318 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1319
1320out:
1321 mutex_unlock(&widget->codec->mutex);
1322 return ret;
1323}
1324EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1325
1326/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001327 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1328 * callback
1329 * @kcontrol: mixer control
1330 * @ucontrol: control element information
1331 *
1332 * Callback to get the value of a dapm semi enumerated double mixer control.
1333 *
1334 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1335 * used for handling bitfield coded enumeration for example.
1336 *
1337 * Returns 0 for success.
1338 */
1339int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
1341{
1342 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001343 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001344 unsigned short reg_val, val, mux;
1345
1346 reg_val = snd_soc_read(widget->codec, e->reg);
1347 val = (reg_val >> e->shift_l) & e->mask;
1348 for (mux = 0; mux < e->max; mux++) {
1349 if (val == e->values[mux])
1350 break;
1351 }
1352 ucontrol->value.enumerated.item[0] = mux;
1353 if (e->shift_l != e->shift_r) {
1354 val = (reg_val >> e->shift_r) & e->mask;
1355 for (mux = 0; mux < e->max; mux++) {
1356 if (val == e->values[mux])
1357 break;
1358 }
1359 ucontrol->value.enumerated.item[1] = mux;
1360 }
1361
1362 return 0;
1363}
1364EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1365
1366/**
1367 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1368 * callback
1369 * @kcontrol: mixer control
1370 * @ucontrol: control element information
1371 *
1372 * Callback to set the value of a dapm semi enumerated double mixer control.
1373 *
1374 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1375 * used for handling bitfield coded enumeration for example.
1376 *
1377 * Returns 0 for success.
1378 */
1379int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1380 struct snd_ctl_elem_value *ucontrol)
1381{
1382 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001383 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001384 unsigned short val, mux;
1385 unsigned short mask;
1386 int ret = 0;
1387
1388 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1389 return -EINVAL;
1390 mux = ucontrol->value.enumerated.item[0];
1391 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1392 mask = e->mask << e->shift_l;
1393 if (e->shift_l != e->shift_r) {
1394 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1395 return -EINVAL;
1396 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1397 mask |= e->mask << e->shift_r;
1398 }
1399
1400 mutex_lock(&widget->codec->mutex);
1401 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001402 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001403 if (widget->event) {
1404 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1405 ret = widget->event(widget,
1406 kcontrol, SND_SOC_DAPM_PRE_REG);
1407 if (ret < 0)
1408 goto out;
1409 }
1410 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1411 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1412 ret = widget->event(widget,
1413 kcontrol, SND_SOC_DAPM_POST_REG);
1414 } else
1415 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1416
1417out:
1418 mutex_unlock(&widget->codec->mutex);
1419 return ret;
1420}
1421EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1422
1423/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001424 * snd_soc_dapm_new_control - create new dapm control
1425 * @codec: audio codec
1426 * @widget: widget template
1427 *
1428 * Creates a new dapm control based upon the template.
1429 *
1430 * Returns 0 for success else error.
1431 */
1432int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1433 const struct snd_soc_dapm_widget *widget)
1434{
1435 struct snd_soc_dapm_widget *w;
1436
1437 if ((w = dapm_cnew_widget(widget)) == NULL)
1438 return -ENOMEM;
1439
1440 w->codec = codec;
1441 INIT_LIST_HEAD(&w->sources);
1442 INIT_LIST_HEAD(&w->sinks);
1443 INIT_LIST_HEAD(&w->list);
1444 list_add(&w->list, &codec->dapm_widgets);
1445
1446 /* machine layer set ups unconnected pins and insertions */
1447 w->connected = 1;
1448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1451
1452/**
Mark Brown4ba13272008-05-13 14:51:19 +02001453 * snd_soc_dapm_new_controls - create new dapm controls
1454 * @codec: audio codec
1455 * @widget: widget array
1456 * @num: number of widgets
1457 *
1458 * Creates new DAPM controls based upon the templates.
1459 *
1460 * Returns 0 for success else error.
1461 */
1462int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1463 const struct snd_soc_dapm_widget *widget,
1464 int num)
1465{
1466 int i, ret;
1467
1468 for (i = 0; i < num; i++) {
1469 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001470 if (ret < 0) {
1471 printk(KERN_ERR
1472 "ASoC: Failed to create DAPM control %s: %d\n",
1473 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001474 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001475 }
Mark Brown4ba13272008-05-13 14:51:19 +02001476 widget++;
1477 }
1478 return 0;
1479}
1480EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1481
1482
1483/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1485 * @codec: audio codec
1486 * @stream: stream name
1487 * @event: stream event
1488 *
1489 * Sends a stream event to the dapm core. The core then makes any
1490 * necessary widget power changes.
1491 *
1492 * Returns 0 for success else error.
1493 */
1494int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1495 char *stream, int event)
1496{
1497 struct snd_soc_dapm_widget *w;
1498
Seth Forshee11da21a2007-02-02 17:14:19 +01001499 if (stream == NULL)
1500 return 0;
1501
Richard Purdie2b97eab2006-10-06 18:32:18 +02001502 mutex_lock(&codec->mutex);
1503 list_for_each_entry(w, &codec->dapm_widgets, list)
1504 {
1505 if (!w->sname)
1506 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001507 pr_debug("widget %s\n %s stream %s event %d\n",
1508 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509 if (strstr(w->sname, stream)) {
1510 switch(event) {
1511 case SND_SOC_DAPM_STREAM_START:
1512 w->active = 1;
1513 break;
1514 case SND_SOC_DAPM_STREAM_STOP:
1515 w->active = 0;
1516 break;
1517 case SND_SOC_DAPM_STREAM_SUSPEND:
1518 if (w->active)
1519 w->suspend = 1;
1520 w->active = 0;
1521 break;
1522 case SND_SOC_DAPM_STREAM_RESUME:
1523 if (w->suspend) {
1524 w->active = 1;
1525 w->suspend = 0;
1526 }
1527 break;
1528 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1529 break;
1530 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1531 break;
1532 }
1533 }
1534 }
1535 mutex_unlock(&codec->mutex);
1536
1537 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001538 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001539 return 0;
1540}
1541EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1542
1543/**
Mark Brown0be98982008-05-19 12:31:28 +02001544 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001545 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001546 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001547 *
Mark Brown0be98982008-05-19 12:31:28 +02001548 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001549 *
1550 * Returns 0 for success else error.
1551 */
Mark Brown0be98982008-05-19 12:31:28 +02001552int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1553 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001554{
Mark Brown87506542008-11-18 20:50:34 +00001555 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001556 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown0be98982008-05-19 12:31:28 +02001557 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001558
Mark Brown87506542008-11-18 20:50:34 +00001559 if (card->set_bias_level)
1560 ret = card->set_bias_level(card, level);
Mark Brown0be98982008-05-19 12:31:28 +02001561 if (ret == 0 && codec->set_bias_level)
1562 ret = codec->set_bias_level(codec, level);
1563
1564 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001565}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001566
1567/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001568 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001569 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001570 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001572 * Enables input/output pin and it's parents or children widgets iff there is
1573 * a valid audio route and active audio stream.
1574 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1575 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001576 */
Mark Brown16499232009-01-07 18:25:13 +00001577int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578{
Liam Girdwooda5302182008-07-07 13:35:17 +01001579 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001580}
Liam Girdwooda5302182008-07-07 13:35:17 +01001581EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001582
1583/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001584 * snd_soc_dapm_disable_pin - disable pin.
1585 * @codec: SoC codec
1586 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001587 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001588 * Disables input/output pin and it's parents or children widgets.
1589 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1590 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001591 */
Mark Brown16499232009-01-07 18:25:13 +00001592int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001593{
1594 return snd_soc_dapm_set_pin(codec, pin, 0);
1595}
1596EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1597
1598/**
Mark Brown5817b522008-09-24 11:23:11 +01001599 * snd_soc_dapm_nc_pin - permanently disable pin.
1600 * @codec: SoC codec
1601 * @pin: pin name
1602 *
1603 * Marks the specified pin as being not connected, disabling it along
1604 * any parent or child widgets. At present this is identical to
1605 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1606 * additional things such as disabling controls which only affect
1607 * paths through the pin.
1608 *
1609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1610 * do any widget power switching.
1611 */
Mark Brown16499232009-01-07 18:25:13 +00001612int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001613{
1614 return snd_soc_dapm_set_pin(codec, pin, 0);
1615}
1616EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1617
1618/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001619 * snd_soc_dapm_get_pin_status - get audio pin status
1620 * @codec: audio codec
1621 * @pin: audio signal pin endpoint (or start point)
1622 *
1623 * Get audio pin status - connected or disconnected.
1624 *
1625 * Returns 1 for connected otherwise 0.
1626 */
Mark Brown16499232009-01-07 18:25:13 +00001627int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001628{
1629 struct snd_soc_dapm_widget *w;
1630
1631 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001632 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001633 return w->connected;
1634 }
1635
1636 return 0;
1637}
Liam Girdwooda5302182008-07-07 13:35:17 +01001638EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001639
1640/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001641 * snd_soc_dapm_free - free dapm resources
1642 * @socdev: SoC device
1643 *
1644 * Free all dapm widgets and resources.
1645 */
1646void snd_soc_dapm_free(struct snd_soc_device *socdev)
1647{
Mark Brown6627a652009-01-23 22:55:23 +00001648 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649
1650 snd_soc_dapm_sys_remove(socdev->dev);
1651 dapm_free_widgets(codec);
1652}
1653EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1654
1655/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001656MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001657MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1658MODULE_LICENSE("GPL");