blob: 28e6e324ccfbb5815ec0cb5f3a710f716b92b5e4 [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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-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;
Mark Brown07495f32009-03-05 17:06:23 +0000335 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000336 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) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000344 default:
345 snprintf(path->long_name, name_len, "%s %s",
346 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000347 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000348 case snd_soc_dapm_mixer_named_ctl:
349 snprintf(path->long_name, name_len, "%s",
350 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000351 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000352 }
353
Mark Brown219b93f2008-10-28 13:02:31 +0000354 path->long_name[name_len - 1] = '\0';
355
Richard Purdie2b97eab2006-10-06 18:32:18 +0200356 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
357 path->long_name);
358 ret = snd_ctl_add(codec->card, path->kcontrol);
359 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100360 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
361 path->long_name,
362 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363 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
Mark Brown025756ec2009-04-13 11:09:18 +0100525/* Standard power change method, used to apply power changes to most
526 * widgets.
527 */
528static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
529{
530 int ret;
531
532 /* call any power change event handlers */
533 if (w->event)
534 pr_debug("power %s event for %s flags %x\n",
535 w->power ? "on" : "off",
536 w->name, w->event_flags);
537
538 /* power up pre event */
539 if (w->power && w->event &&
540 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
541 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
542 if (ret < 0)
543 return ret;
544 }
545
546 /* power down pre event */
547 if (!w->power && w->event &&
548 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
549 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
550 if (ret < 0)
551 return ret;
552 }
553
554 /* Lower PGA volume to reduce pops */
555 if (w->id == snd_soc_dapm_pga && !w->power)
556 dapm_set_pga(w, w->power);
557
558 dapm_update_bits(w);
559
560 /* Raise PGA volume to reduce pops */
561 if (w->id == snd_soc_dapm_pga && w->power)
562 dapm_set_pga(w, w->power);
563
564 /* power up post event */
565 if (w->power && w->event &&
566 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
567 ret = w->event(w,
568 NULL, SND_SOC_DAPM_POST_PMU);
569 if (ret < 0)
570 return ret;
571 }
572
573 /* power down post event */
574 if (!w->power && w->event &&
575 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
576 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
577 if (ret < 0)
578 return ret;
579 }
580
581 return 0;
582}
583
Mark Browncd0f2d42009-04-20 16:56:59 +0100584/* Generic check to see if a widget should be powered.
585 */
586static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
587{
588 int in, out;
589
590 in = is_connected_input_ep(w);
591 dapm_clear_walk(w->codec);
592 out = is_connected_output_ep(w);
593 dapm_clear_walk(w->codec);
594 return out != 0 && in != 0;
595}
596
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300597/*
Mark Brown42aa3412009-03-01 19:21:10 +0000598 * Scan a single DAPM widget for a complete audio path and update the
599 * power status appropriately.
600 */
601static int dapm_power_widget(struct snd_soc_codec *codec, int event,
602 struct snd_soc_dapm_widget *w)
603{
604 int in, out, power_change, power, ret;
605
606 /* vmid - no action */
607 if (w->id == snd_soc_dapm_vmid)
608 return 0;
609
610 /* active ADC */
611 if (w->id == snd_soc_dapm_adc && w->active) {
612 in = is_connected_input_ep(w);
613 dapm_clear_walk(w->codec);
Mark Brownf6d655a2009-04-13 11:27:03 +0100614 power = (in != 0) ? 1 : 0;
615 if (power == w->power)
616 return 0;
617 w->power = power;
618 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000619 }
620
621 /* active DAC */
622 if (w->id == snd_soc_dapm_dac && w->active) {
623 out = is_connected_output_ep(w);
624 dapm_clear_walk(w->codec);
Mark Brownf6d655a2009-04-13 11:27:03 +0100625 power = (out != 0) ? 1 : 0;
626 if (power == w->power)
627 return 0;
628 w->power = power;
629 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000630 }
631
632 /* pre and post event widgets */
633 if (w->id == snd_soc_dapm_pre) {
634 if (!w->event)
635 return 0;
636
637 if (event == SND_SOC_DAPM_STREAM_START) {
638 ret = w->event(w,
639 NULL, SND_SOC_DAPM_PRE_PMU);
640 if (ret < 0)
641 return ret;
642 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
643 ret = w->event(w,
644 NULL, SND_SOC_DAPM_PRE_PMD);
645 if (ret < 0)
646 return ret;
647 }
648 return 0;
649 }
650 if (w->id == snd_soc_dapm_post) {
651 if (!w->event)
652 return 0;
653
654 if (event == SND_SOC_DAPM_STREAM_START) {
655 ret = w->event(w,
656 NULL, SND_SOC_DAPM_POST_PMU);
657 if (ret < 0)
658 return ret;
659 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
660 ret = w->event(w,
661 NULL, SND_SOC_DAPM_POST_PMD);
662 if (ret < 0)
663 return ret;
664 }
665 return 0;
666 }
667
668 /* all other widgets */
Mark Browncd0f2d42009-04-20 16:56:59 +0100669 power = dapm_generic_check_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000670 power_change = (w->power == power) ? 0 : 1;
671 w->power = power;
672
673 if (!power_change)
674 return 0;
675
Mark Brown025756ec2009-04-13 11:09:18 +0100676 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000677}
678
679/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200680 * Scan each dapm widget for complete audio path.
681 * A complete path is a route that has valid endpoints i.e.:-
682 *
683 * o DAC to output pin.
684 * o Input Pin to ADC.
685 * o Input pin to Output pin (bypass, sidetone)
686 * o DAC to ADC (loopback).
687 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100688static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200689{
690 struct snd_soc_dapm_widget *w;
Mark Brown42aa3412009-03-01 19:21:10 +0000691 int i, c = 1, *seq = NULL, ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200692
693 /* do we have a sequenced stream event */
694 if (event == SND_SOC_DAPM_STREAM_START) {
695 c = ARRAY_SIZE(dapm_up_seq);
696 seq = dapm_up_seq;
697 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
698 c = ARRAY_SIZE(dapm_down_seq);
699 seq = dapm_down_seq;
700 }
701
Mark Brown42aa3412009-03-01 19:21:10 +0000702 for (i = 0; i < c; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 list_for_each_entry(w, &codec->dapm_widgets, list) {
704
705 /* is widget in stream order */
706 if (seq && seq[i] && w->id != seq[i])
707 continue;
708
Mark Brown42aa3412009-03-01 19:21:10 +0000709 ret = dapm_power_widget(codec, event, w);
710 if (ret != 0)
711 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712 }
713 }
714
Mark Brown42aa3412009-03-01 19:21:10 +0000715 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716}
717
Mark Brownc1286b82008-07-07 19:26:03 +0100718#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200719static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
720{
721 struct snd_soc_dapm_widget *w;
722 struct snd_soc_dapm_path *p = NULL;
723 int in, out;
724
725 printk("DAPM %s %s\n", codec->name, action);
726
727 list_for_each_entry(w, &codec->dapm_widgets, list) {
728
729 /* only display widgets that effect routing */
730 switch (w->id) {
731 case snd_soc_dapm_pre:
732 case snd_soc_dapm_post:
733 case snd_soc_dapm_vmid:
734 continue;
735 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200736 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737 case snd_soc_dapm_output:
738 case snd_soc_dapm_input:
739 case snd_soc_dapm_switch:
740 case snd_soc_dapm_hp:
741 case snd_soc_dapm_mic:
742 case snd_soc_dapm_spk:
743 case snd_soc_dapm_line:
744 case snd_soc_dapm_micbias:
745 case snd_soc_dapm_dac:
746 case snd_soc_dapm_adc:
747 case snd_soc_dapm_pga:
748 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000749 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 if (w->name) {
751 in = is_connected_input_ep(w);
752 dapm_clear_walk(w->codec);
753 out = is_connected_output_ep(w);
754 dapm_clear_walk(w->codec);
755 printk("%s: %s in %d out %d\n", w->name,
756 w->power ? "On":"Off",in, out);
757
758 list_for_each_entry(p, &w->sources, list_sink) {
759 if (p->connect)
760 printk(" in %s %s\n", p->name ? p->name : "static",
761 p->source->name);
762 }
763 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200764 if (p->connect)
765 printk(" out %s %s\n", p->name ? p->name : "static",
766 p->sink->name);
767 }
768 }
769 break;
770 }
771 }
772}
773#endif
774
775/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100776static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
777 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800778 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200779{
780 struct snd_soc_dapm_path *path;
781 int found = 0;
782
Peter Ujfalusieff317d2009-01-15 14:40:47 +0200783 if (widget->id != snd_soc_dapm_mux &&
784 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 return -ENODEV;
786
787 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
788 return 0;
789
790 /* find dapm widget path assoc with kcontrol */
791 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
792 if (path->kcontrol != kcontrol)
793 continue;
794
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800795 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796 continue;
797
798 found = 1;
799 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800800 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801 path->connect = 1; /* new connection */
802 else
803 path->connect = 0; /* old connection must be powered down */
804 }
805
Mark Brown3fccd8b2008-07-07 19:26:04 +0100806 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100808 dump_dapm(widget->codec, "mux power update");
809 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810
811 return 0;
812}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200813
Milan plzik1b075e32008-01-10 14:39:46 +0100814/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100815static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
816 struct snd_kcontrol *kcontrol, int reg,
817 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200818{
819 struct snd_soc_dapm_path *path;
820 int found = 0;
821
Milan plzik1b075e32008-01-10 14:39:46 +0100822 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000823 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100824 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825 return -ENODEV;
826
827 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
828 return 0;
829
830 /* find dapm widget path assoc with kcontrol */
831 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
832 if (path->kcontrol != kcontrol)
833 continue;
834
835 /* found, now check type */
836 found = 1;
837 if (val)
838 /* new connection */
839 path->connect = invert ? 0:1;
840 else
841 /* old connection must be powered down */
842 path->connect = invert ? 1:0;
843 break;
844 }
845
Mark Brown3fccd8b2008-07-07 19:26:04 +0100846 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100848 dump_dapm(widget->codec, "mixer power update");
849 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200850
851 return 0;
852}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200853
854/* show dapm widget status in sys fs */
855static ssize_t dapm_widget_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
857{
858 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +0000859 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860 struct snd_soc_dapm_widget *w;
861 int count = 0;
862 char *state = "not set";
863
864 list_for_each_entry(w, &codec->dapm_widgets, list) {
865
866 /* only display widgets that burnm power */
867 switch (w->id) {
868 case snd_soc_dapm_hp:
869 case snd_soc_dapm_mic:
870 case snd_soc_dapm_spk:
871 case snd_soc_dapm_line:
872 case snd_soc_dapm_micbias:
873 case snd_soc_dapm_dac:
874 case snd_soc_dapm_adc:
875 case snd_soc_dapm_pga:
876 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000877 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200878 if (w->name)
879 count += sprintf(buf + count, "%s: %s\n",
880 w->name, w->power ? "On":"Off");
881 break;
882 default:
883 break;
884 }
885 }
886
Mark Brown0be98982008-05-19 12:31:28 +0200887 switch (codec->bias_level) {
888 case SND_SOC_BIAS_ON:
889 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200890 break;
Mark Brown0be98982008-05-19 12:31:28 +0200891 case SND_SOC_BIAS_PREPARE:
892 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200893 break;
Mark Brown0be98982008-05-19 12:31:28 +0200894 case SND_SOC_BIAS_STANDBY:
895 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 break;
Mark Brown0be98982008-05-19 12:31:28 +0200897 case SND_SOC_BIAS_OFF:
898 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200899 break;
900 }
901 count += sprintf(buf + count, "PM State: %s\n", state);
902
903 return count;
904}
905
906static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
907
908int snd_soc_dapm_sys_add(struct device *dev)
909{
Mark Brownf0062a92008-08-28 12:46:24 +0100910 if (!dapm_status)
911 return 0;
Troy Kisky12ef1932008-10-13 17:42:14 -0700912 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913}
914
915static void snd_soc_dapm_sys_remove(struct device *dev)
916{
Mark Brown15e4c72f2008-07-02 11:51:20 +0100917 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200918 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100919 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200920}
921
922/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100923static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924{
925 struct snd_soc_dapm_widget *w, *next_w;
926 struct snd_soc_dapm_path *p, *next_p;
927
928 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
929 list_del(&w->list);
930 kfree(w);
931 }
932
933 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
934 list_del(&p->list);
935 kfree(p->long_name);
936 kfree(p);
937 }
938}
939
Liam Girdwooda5302182008-07-07 13:35:17 +0100940static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +0000941 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +0100942{
943 struct snd_soc_dapm_widget *w;
944
945 list_for_each_entry(w, &codec->dapm_widgets, list) {
946 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100947 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100948 w->connected = status;
949 return 0;
950 }
951 }
952
Mark Brownc1286b82008-07-07 19:26:03 +0100953 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100954 return -EINVAL;
955}
956
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100958 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200959 * @codec: audio codec
960 *
961 * Walks all dapm audio paths and powers widgets according to their
962 * stream or path usage.
963 *
964 * Returns 0 for success.
965 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100966int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200967{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100968 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
969 dump_dapm(codec, "sync");
970 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200971}
Liam Girdwooda5302182008-07-07 13:35:17 +0100972EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200973
Mark Brown105f1c22008-05-13 14:52:19 +0200974static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
975 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976{
977 struct snd_soc_dapm_path *path;
978 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
979 int ret = 0;
980
981 /* find src and dest widgets */
982 list_for_each_entry(w, &codec->dapm_widgets, list) {
983
984 if (!wsink && !(strcmp(w->name, sink))) {
985 wsink = w;
986 continue;
987 }
988 if (!wsource && !(strcmp(w->name, source))) {
989 wsource = w;
990 }
991 }
992
993 if (wsource == NULL || wsink == NULL)
994 return -ENODEV;
995
996 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
997 if (!path)
998 return -ENOMEM;
999
1000 path->source = wsource;
1001 path->sink = wsink;
1002 INIT_LIST_HEAD(&path->list);
1003 INIT_LIST_HEAD(&path->list_source);
1004 INIT_LIST_HEAD(&path->list_sink);
1005
1006 /* check for external widgets */
1007 if (wsink->id == snd_soc_dapm_input) {
1008 if (wsource->id == snd_soc_dapm_micbias ||
1009 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001010 wsink->id == snd_soc_dapm_line ||
1011 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001012 wsink->ext = 1;
1013 }
1014 if (wsource->id == snd_soc_dapm_output) {
1015 if (wsink->id == snd_soc_dapm_spk ||
1016 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001017 wsink->id == snd_soc_dapm_line ||
1018 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001019 wsource->ext = 1;
1020 }
1021
1022 /* connect static paths */
1023 if (control == NULL) {
1024 list_add(&path->list, &codec->dapm_paths);
1025 list_add(&path->list_sink, &wsink->sources);
1026 list_add(&path->list_source, &wsource->sinks);
1027 path->connect = 1;
1028 return 0;
1029 }
1030
1031 /* connect dynamic paths */
1032 switch(wsink->id) {
1033 case snd_soc_dapm_adc:
1034 case snd_soc_dapm_dac:
1035 case snd_soc_dapm_pga:
1036 case snd_soc_dapm_input:
1037 case snd_soc_dapm_output:
1038 case snd_soc_dapm_micbias:
1039 case snd_soc_dapm_vmid:
1040 case snd_soc_dapm_pre:
1041 case snd_soc_dapm_post:
1042 list_add(&path->list, &codec->dapm_paths);
1043 list_add(&path->list_sink, &wsink->sources);
1044 list_add(&path->list_source, &wsource->sinks);
1045 path->connect = 1;
1046 return 0;
1047 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001048 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001049 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1050 &wsink->kcontrols[0]);
1051 if (ret != 0)
1052 goto err;
1053 break;
1054 case snd_soc_dapm_switch:
1055 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001056 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001057 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1058 if (ret != 0)
1059 goto err;
1060 break;
1061 case snd_soc_dapm_hp:
1062 case snd_soc_dapm_mic:
1063 case snd_soc_dapm_line:
1064 case snd_soc_dapm_spk:
1065 list_add(&path->list, &codec->dapm_paths);
1066 list_add(&path->list_sink, &wsink->sources);
1067 list_add(&path->list_source, &wsource->sinks);
1068 path->connect = 0;
1069 return 0;
1070 }
1071 return 0;
1072
1073err:
1074 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1075 control, sink);
1076 kfree(path);
1077 return ret;
1078}
Mark Brown105f1c22008-05-13 14:52:19 +02001079
1080/**
Mark Brown105f1c22008-05-13 14:52:19 +02001081 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1082 * @codec: codec
1083 * @route: audio routes
1084 * @num: number of routes
1085 *
1086 * Connects 2 dapm widgets together via a named audio path. The sink is
1087 * the widget receiving the audio signal, whilst the source is the sender
1088 * of the audio signal.
1089 *
1090 * Returns 0 for success else error. On error all resources can be freed
1091 * with a call to snd_soc_card_free().
1092 */
1093int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1094 const struct snd_soc_dapm_route *route, int num)
1095{
1096 int i, ret;
1097
1098 for (i = 0; i < num; i++) {
1099 ret = snd_soc_dapm_add_route(codec, route->sink,
1100 route->control, route->source);
1101 if (ret < 0) {
1102 printk(KERN_ERR "Failed to add route %s->%s\n",
1103 route->source,
1104 route->sink);
1105 return ret;
1106 }
1107 route++;
1108 }
1109
1110 return 0;
1111}
1112EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1113
1114/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001115 * snd_soc_dapm_new_widgets - add new dapm widgets
1116 * @codec: audio codec
1117 *
1118 * Checks the codec for any new dapm widgets and creates them if found.
1119 *
1120 * Returns 0 for success.
1121 */
1122int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1123{
1124 struct snd_soc_dapm_widget *w;
1125
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126 list_for_each_entry(w, &codec->dapm_widgets, list)
1127 {
1128 if (w->new)
1129 continue;
1130
1131 switch(w->id) {
1132 case snd_soc_dapm_switch:
1133 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001134 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001135 dapm_new_mixer(codec, w);
1136 break;
1137 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001138 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139 dapm_new_mux(codec, w);
1140 break;
1141 case snd_soc_dapm_adc:
1142 case snd_soc_dapm_dac:
1143 case snd_soc_dapm_pga:
1144 dapm_new_pga(codec, w);
1145 break;
1146 case snd_soc_dapm_input:
1147 case snd_soc_dapm_output:
1148 case snd_soc_dapm_micbias:
1149 case snd_soc_dapm_spk:
1150 case snd_soc_dapm_hp:
1151 case snd_soc_dapm_mic:
1152 case snd_soc_dapm_line:
1153 case snd_soc_dapm_vmid:
1154 case snd_soc_dapm_pre:
1155 case snd_soc_dapm_post:
1156 break;
1157 }
1158 w->new = 1;
1159 }
1160
1161 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162 return 0;
1163}
1164EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1165
1166/**
1167 * snd_soc_dapm_get_volsw - dapm mixer get callback
1168 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001169 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001170 *
1171 * Callback to get the value of a dapm mixer control.
1172 *
1173 * Returns 0 for success.
1174 */
1175int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1176 struct snd_ctl_elem_value *ucontrol)
1177{
1178 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001179 struct soc_mixer_control *mc =
1180 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001181 unsigned int reg = mc->reg;
1182 unsigned int shift = mc->shift;
1183 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001184 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001185 unsigned int invert = mc->invert;
1186 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187
1188 /* return the saved value if we are powered down */
1189 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1190 ucontrol->value.integer.value[0] = widget->saved_value;
1191 return 0;
1192 }
1193
1194 ucontrol->value.integer.value[0] =
1195 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1196 if (shift != rshift)
1197 ucontrol->value.integer.value[1] =
1198 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1199 if (invert) {
1200 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001201 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001202 if (shift != rshift)
1203 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001204 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001205 }
1206
1207 return 0;
1208}
1209EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1210
1211/**
1212 * snd_soc_dapm_put_volsw - dapm mixer set callback
1213 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001214 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001215 *
1216 * Callback to set the value of a dapm mixer control.
1217 *
1218 * Returns 0 for success.
1219 */
1220int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1221 struct snd_ctl_elem_value *ucontrol)
1222{
1223 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001224 struct soc_mixer_control *mc =
1225 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001226 unsigned int reg = mc->reg;
1227 unsigned int shift = mc->shift;
1228 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001229 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001230 unsigned int mask = (1 << fls(max)) - 1;
1231 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001232 unsigned short val, val2, val_mask;
1233 int ret;
1234
1235 val = (ucontrol->value.integer.value[0] & mask);
1236
1237 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001238 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001239 val_mask = mask << shift;
1240 val = val << shift;
1241 if (shift != rshift) {
1242 val2 = (ucontrol->value.integer.value[1] & mask);
1243 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001244 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001245 val_mask |= mask << rshift;
1246 val |= val2 << rshift;
1247 }
1248
1249 mutex_lock(&widget->codec->mutex);
1250 widget->value = val;
1251
1252 /* save volume value if the widget is powered down */
1253 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1254 widget->saved_value = val;
1255 mutex_unlock(&widget->codec->mutex);
1256 return 1;
1257 }
1258
1259 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1260 if (widget->event) {
1261 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001262 ret = widget->event(widget, kcontrol,
1263 SND_SOC_DAPM_PRE_REG);
1264 if (ret < 0) {
1265 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001267 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001268 }
1269 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1270 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001271 ret = widget->event(widget, kcontrol,
1272 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 } else
1274 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1275
1276out:
1277 mutex_unlock(&widget->codec->mutex);
1278 return ret;
1279}
1280EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1281
1282/**
1283 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1284 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001285 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001286 *
1287 * Callback to get the value of a dapm enumerated double mixer control.
1288 *
1289 * Returns 0 for success.
1290 */
1291int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1292 struct snd_ctl_elem_value *ucontrol)
1293{
1294 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1295 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1296 unsigned short val, bitmask;
1297
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001298 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299 ;
1300 val = snd_soc_read(widget->codec, e->reg);
1301 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1302 if (e->shift_l != e->shift_r)
1303 ucontrol->value.enumerated.item[1] =
1304 (val >> e->shift_r) & (bitmask - 1);
1305
1306 return 0;
1307}
1308EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1309
1310/**
1311 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1312 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001313 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001314 *
1315 * Callback to set the value of a dapm enumerated double mixer control.
1316 *
1317 * Returns 0 for success.
1318 */
1319int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1320 struct snd_ctl_elem_value *ucontrol)
1321{
1322 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1323 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1324 unsigned short val, mux;
1325 unsigned short mask, bitmask;
1326 int ret = 0;
1327
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001328 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001329 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001330 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001331 return -EINVAL;
1332 mux = ucontrol->value.enumerated.item[0];
1333 val = mux << e->shift_l;
1334 mask = (bitmask - 1) << e->shift_l;
1335 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001336 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001337 return -EINVAL;
1338 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1339 mask |= (bitmask - 1) << e->shift_r;
1340 }
1341
1342 mutex_lock(&widget->codec->mutex);
1343 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001344 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001345 if (widget->event) {
1346 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001347 ret = widget->event(widget,
1348 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001349 if (ret < 0)
1350 goto out;
1351 }
1352 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1353 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001354 ret = widget->event(widget,
1355 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001356 } else
1357 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1358
1359out:
1360 mutex_unlock(&widget->codec->mutex);
1361 return ret;
1362}
1363EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1364
1365/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001366 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1367 * callback
1368 * @kcontrol: mixer control
1369 * @ucontrol: control element information
1370 *
1371 * Callback to get the value of a dapm semi enumerated double mixer control.
1372 *
1373 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1374 * used for handling bitfield coded enumeration for example.
1375 *
1376 * Returns 0 for success.
1377 */
1378int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1379 struct snd_ctl_elem_value *ucontrol)
1380{
1381 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001382 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001383 unsigned short reg_val, val, mux;
1384
1385 reg_val = snd_soc_read(widget->codec, e->reg);
1386 val = (reg_val >> e->shift_l) & e->mask;
1387 for (mux = 0; mux < e->max; mux++) {
1388 if (val == e->values[mux])
1389 break;
1390 }
1391 ucontrol->value.enumerated.item[0] = mux;
1392 if (e->shift_l != e->shift_r) {
1393 val = (reg_val >> e->shift_r) & e->mask;
1394 for (mux = 0; mux < e->max; mux++) {
1395 if (val == e->values[mux])
1396 break;
1397 }
1398 ucontrol->value.enumerated.item[1] = mux;
1399 }
1400
1401 return 0;
1402}
1403EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1404
1405/**
1406 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1407 * callback
1408 * @kcontrol: mixer control
1409 * @ucontrol: control element information
1410 *
1411 * Callback to set the value of a dapm semi enumerated double mixer control.
1412 *
1413 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1414 * used for handling bitfield coded enumeration for example.
1415 *
1416 * Returns 0 for success.
1417 */
1418int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1419 struct snd_ctl_elem_value *ucontrol)
1420{
1421 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001422 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001423 unsigned short val, mux;
1424 unsigned short mask;
1425 int ret = 0;
1426
1427 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1428 return -EINVAL;
1429 mux = ucontrol->value.enumerated.item[0];
1430 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1431 mask = e->mask << e->shift_l;
1432 if (e->shift_l != e->shift_r) {
1433 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1434 return -EINVAL;
1435 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1436 mask |= e->mask << e->shift_r;
1437 }
1438
1439 mutex_lock(&widget->codec->mutex);
1440 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001441 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001442 if (widget->event) {
1443 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1444 ret = widget->event(widget,
1445 kcontrol, SND_SOC_DAPM_PRE_REG);
1446 if (ret < 0)
1447 goto out;
1448 }
1449 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1450 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1451 ret = widget->event(widget,
1452 kcontrol, SND_SOC_DAPM_POST_REG);
1453 } else
1454 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1455
1456out:
1457 mutex_unlock(&widget->codec->mutex);
1458 return ret;
1459}
1460EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1461
1462/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001463 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1464 *
1465 * @kcontrol: mixer control
1466 * @uinfo: control element information
1467 *
1468 * Callback to provide information about a pin switch control.
1469 */
1470int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_info *uinfo)
1472{
1473 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1474 uinfo->count = 1;
1475 uinfo->value.integer.min = 0;
1476 uinfo->value.integer.max = 1;
1477
1478 return 0;
1479}
1480EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1481
1482/**
1483 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1484 *
1485 * @kcontrol: mixer control
1486 * @ucontrol: Value
1487 */
1488int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_value *ucontrol)
1490{
1491 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1492 const char *pin = (const char *)kcontrol->private_value;
1493
1494 mutex_lock(&codec->mutex);
1495
1496 ucontrol->value.integer.value[0] =
1497 snd_soc_dapm_get_pin_status(codec, pin);
1498
1499 mutex_unlock(&codec->mutex);
1500
1501 return 0;
1502}
1503EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1504
1505/**
1506 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1507 *
1508 * @kcontrol: mixer control
1509 * @ucontrol: Value
1510 */
1511int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_value *ucontrol)
1513{
1514 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1515 const char *pin = (const char *)kcontrol->private_value;
1516
1517 mutex_lock(&codec->mutex);
1518
1519 if (ucontrol->value.integer.value[0])
1520 snd_soc_dapm_enable_pin(codec, pin);
1521 else
1522 snd_soc_dapm_disable_pin(codec, pin);
1523
1524 snd_soc_dapm_sync(codec);
1525
1526 mutex_unlock(&codec->mutex);
1527
1528 return 0;
1529}
1530EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1531
1532/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001533 * snd_soc_dapm_new_control - create new dapm control
1534 * @codec: audio codec
1535 * @widget: widget template
1536 *
1537 * Creates a new dapm control based upon the template.
1538 *
1539 * Returns 0 for success else error.
1540 */
1541int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1542 const struct snd_soc_dapm_widget *widget)
1543{
1544 struct snd_soc_dapm_widget *w;
1545
1546 if ((w = dapm_cnew_widget(widget)) == NULL)
1547 return -ENOMEM;
1548
1549 w->codec = codec;
1550 INIT_LIST_HEAD(&w->sources);
1551 INIT_LIST_HEAD(&w->sinks);
1552 INIT_LIST_HEAD(&w->list);
1553 list_add(&w->list, &codec->dapm_widgets);
1554
1555 /* machine layer set ups unconnected pins and insertions */
1556 w->connected = 1;
1557 return 0;
1558}
1559EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1560
1561/**
Mark Brown4ba13272008-05-13 14:51:19 +02001562 * snd_soc_dapm_new_controls - create new dapm controls
1563 * @codec: audio codec
1564 * @widget: widget array
1565 * @num: number of widgets
1566 *
1567 * Creates new DAPM controls based upon the templates.
1568 *
1569 * Returns 0 for success else error.
1570 */
1571int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1572 const struct snd_soc_dapm_widget *widget,
1573 int num)
1574{
1575 int i, ret;
1576
1577 for (i = 0; i < num; i++) {
1578 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001579 if (ret < 0) {
1580 printk(KERN_ERR
1581 "ASoC: Failed to create DAPM control %s: %d\n",
1582 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001583 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001584 }
Mark Brown4ba13272008-05-13 14:51:19 +02001585 widget++;
1586 }
1587 return 0;
1588}
1589EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1590
1591
1592/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001593 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1594 * @codec: audio codec
1595 * @stream: stream name
1596 * @event: stream event
1597 *
1598 * Sends a stream event to the dapm core. The core then makes any
1599 * necessary widget power changes.
1600 *
1601 * Returns 0 for success else error.
1602 */
1603int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1604 char *stream, int event)
1605{
1606 struct snd_soc_dapm_widget *w;
1607
Seth Forshee11da21a2007-02-02 17:14:19 +01001608 if (stream == NULL)
1609 return 0;
1610
Richard Purdie2b97eab2006-10-06 18:32:18 +02001611 mutex_lock(&codec->mutex);
1612 list_for_each_entry(w, &codec->dapm_widgets, list)
1613 {
1614 if (!w->sname)
1615 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001616 pr_debug("widget %s\n %s stream %s event %d\n",
1617 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 if (strstr(w->sname, stream)) {
1619 switch(event) {
1620 case SND_SOC_DAPM_STREAM_START:
1621 w->active = 1;
1622 break;
1623 case SND_SOC_DAPM_STREAM_STOP:
1624 w->active = 0;
1625 break;
1626 case SND_SOC_DAPM_STREAM_SUSPEND:
1627 if (w->active)
1628 w->suspend = 1;
1629 w->active = 0;
1630 break;
1631 case SND_SOC_DAPM_STREAM_RESUME:
1632 if (w->suspend) {
1633 w->active = 1;
1634 w->suspend = 0;
1635 }
1636 break;
1637 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1638 break;
1639 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1640 break;
1641 }
1642 }
1643 }
1644 mutex_unlock(&codec->mutex);
1645
1646 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001647 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001648 return 0;
1649}
1650EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1651
1652/**
Mark Brown0be98982008-05-19 12:31:28 +02001653 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001654 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001655 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001656 *
Mark Brown0be98982008-05-19 12:31:28 +02001657 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001658 *
1659 * Returns 0 for success else error.
1660 */
Mark Brown0be98982008-05-19 12:31:28 +02001661int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1662 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001663{
Mark Brown87506542008-11-18 20:50:34 +00001664 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001665 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown0be98982008-05-19 12:31:28 +02001666 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001667
Mark Brown87506542008-11-18 20:50:34 +00001668 if (card->set_bias_level)
1669 ret = card->set_bias_level(card, level);
Mark Brown0be98982008-05-19 12:31:28 +02001670 if (ret == 0 && codec->set_bias_level)
1671 ret = codec->set_bias_level(codec, level);
1672
1673 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001674}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001675
1676/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001677 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001678 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001679 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001680 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001681 * Enables input/output pin and it's parents or children widgets iff there is
1682 * a valid audio route and active audio stream.
1683 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1684 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001685 */
Mark Brown16499232009-01-07 18:25:13 +00001686int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001687{
Liam Girdwooda5302182008-07-07 13:35:17 +01001688 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689}
Liam Girdwooda5302182008-07-07 13:35:17 +01001690EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691
1692/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001693 * snd_soc_dapm_disable_pin - disable pin.
1694 * @codec: SoC codec
1695 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001696 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001697 * Disables input/output pin and it's parents or children widgets.
1698 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1699 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001700 */
Mark Brown16499232009-01-07 18:25:13 +00001701int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001702{
1703 return snd_soc_dapm_set_pin(codec, pin, 0);
1704}
1705EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1706
1707/**
Mark Brown5817b522008-09-24 11:23:11 +01001708 * snd_soc_dapm_nc_pin - permanently disable pin.
1709 * @codec: SoC codec
1710 * @pin: pin name
1711 *
1712 * Marks the specified pin as being not connected, disabling it along
1713 * any parent or child widgets. At present this is identical to
1714 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1715 * additional things such as disabling controls which only affect
1716 * paths through the pin.
1717 *
1718 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1719 * do any widget power switching.
1720 */
Mark Brown16499232009-01-07 18:25:13 +00001721int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001722{
1723 return snd_soc_dapm_set_pin(codec, pin, 0);
1724}
1725EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1726
1727/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001728 * snd_soc_dapm_get_pin_status - get audio pin status
1729 * @codec: audio codec
1730 * @pin: audio signal pin endpoint (or start point)
1731 *
1732 * Get audio pin status - connected or disconnected.
1733 *
1734 * Returns 1 for connected otherwise 0.
1735 */
Mark Brown16499232009-01-07 18:25:13 +00001736int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001737{
1738 struct snd_soc_dapm_widget *w;
1739
1740 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001741 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001742 return w->connected;
1743 }
1744
1745 return 0;
1746}
Liam Girdwooda5302182008-07-07 13:35:17 +01001747EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001748
1749/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 * snd_soc_dapm_free - free dapm resources
1751 * @socdev: SoC device
1752 *
1753 * Free all dapm widgets and resources.
1754 */
1755void snd_soc_dapm_free(struct snd_soc_device *socdev)
1756{
Mark Brown6627a652009-01-23 22:55:23 +00001757 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001758
1759 snd_soc_dapm_sys_remove(socdev->dev);
1760 dapm_free_widgets(codec);
1761}
1762EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1763
1764/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001765MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1767MODULE_LICENSE("GPL");