blob: c016426fb89672c49b22462ec348dda233d7d9ee [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.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020013 * Features:
14 * o Changes power status of internal codec blocks depending on the
15 * dynamic configuration of codec internal audio paths and active
16 * DAC's/ADC's.
17 * o Platform power domain - can support external components i.e. amps and
18 * mic/meadphone insertion events.
19 * o Automatic Mic Bias support
20 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020022 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020023 * device reopen.
24 *
25 * Todo:
26 * o DAPM power change sequencing - allow for configurable per
27 * codec sequences.
28 * o Support for analogue bias optimisation.
29 * o Support for reduced codec oversampling rates.
30 * o Support for reduced codec bias currents.
31 */
32
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/init.h>
36#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Mark Brownf0062a92008-08-28 12:46:24 +010041#include <linux/debugfs.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/soc-dapm.h>
46#include <sound/initval.h>
47
48/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010049#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020050#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#else
52#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020053#endif
54
Richard Purdie2b97eab2006-10-06 18:32:18 +020055/* dapm power sequences - make this per codec in the future */
56static int dapm_up_seq[] = {
57 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
58 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
59 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
60};
61static int dapm_down_seq[] = {
62 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
63 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
64 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
65};
66
67static int dapm_status = 1;
68module_param(dapm_status, int, 0);
69MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
70
Mark Brownf0062a92008-08-28 12:46:24 +010071static struct dentry *asoc_debugfs;
72
73static u32 pop_time;
Mark Brown15e4c722008-07-02 11:51:20 +010074
75static void pop_wait(void)
76{
77 if (pop_time)
78 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
79}
80
81static void pop_dbg(const char *fmt, ...)
82{
83 va_list args;
84
85 va_start(args, fmt);
86
87 if (pop_time) {
88 vprintk(fmt, args);
89 pop_wait();
90 }
91
92 va_end(args);
93}
94
Richard Purdie2b97eab2006-10-06 18:32:18 +020095/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010096static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020097 const struct snd_soc_dapm_widget *_widget)
98{
Takashi Iwai88cb4292007-02-05 14:56:20 +010099 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200100}
101
102/* set up initial codec paths */
103static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
104 struct snd_soc_dapm_path *p, int i)
105{
106 switch (w->id) {
107 case snd_soc_dapm_switch:
108 case snd_soc_dapm_mixer: {
109 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100110 struct soc_mixer_control *mc = (struct soc_mixer_control *)
111 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400112 unsigned int reg = mc->reg;
113 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100114 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400115 unsigned int mask = (1 << fls(max)) - 1;
116 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200117
118 val = snd_soc_read(w->codec, reg);
119 val = (val >> shift) & mask;
120
121 if ((invert && !val) || (!invert && val))
122 p->connect = 1;
123 else
124 p->connect = 0;
125 }
126 break;
127 case snd_soc_dapm_mux: {
128 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
129 int val, item, bitmask;
130
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200132 ;
133 val = snd_soc_read(w->codec, e->reg);
134 item = (val >> e->shift_l) & (bitmask - 1);
135
136 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100137 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200138 if (!(strcmp(p->name, e->texts[i])) && item == i)
139 p->connect = 1;
140 }
141 }
142 break;
143 /* does not effect routing - always connected */
144 case snd_soc_dapm_pga:
145 case snd_soc_dapm_output:
146 case snd_soc_dapm_adc:
147 case snd_soc_dapm_input:
148 case snd_soc_dapm_dac:
149 case snd_soc_dapm_micbias:
150 case snd_soc_dapm_vmid:
151 p->connect = 1;
152 break;
153 /* does effect routing - dynamically connected */
154 case snd_soc_dapm_hp:
155 case snd_soc_dapm_mic:
156 case snd_soc_dapm_spk:
157 case snd_soc_dapm_line:
158 case snd_soc_dapm_pre:
159 case snd_soc_dapm_post:
160 p->connect = 0;
161 break;
162 }
163}
164
165/* connect mux widget to it's interconnecting audio paths */
166static int dapm_connect_mux(struct snd_soc_codec *codec,
167 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
168 struct snd_soc_dapm_path *path, const char *control_name,
169 const struct snd_kcontrol_new *kcontrol)
170{
171 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
172 int i;
173
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100174 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175 if (!(strcmp(control_name, e->texts[i]))) {
176 list_add(&path->list, &codec->dapm_paths);
177 list_add(&path->list_sink, &dest->sources);
178 list_add(&path->list_source, &src->sinks);
179 path->name = (char*)e->texts[i];
180 dapm_set_path_status(dest, path, 0);
181 return 0;
182 }
183 }
184
185 return -ENODEV;
186}
187
188/* connect mixer widget to it's interconnecting audio paths */
189static int dapm_connect_mixer(struct snd_soc_codec *codec,
190 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
191 struct snd_soc_dapm_path *path, const char *control_name)
192{
193 int i;
194
195 /* search for mixer kcontrol */
196 for (i = 0; i < dest->num_kcontrols; i++) {
197 if (!strcmp(control_name, dest->kcontrols[i].name)) {
198 list_add(&path->list, &codec->dapm_paths);
199 list_add(&path->list_sink, &dest->sources);
200 list_add(&path->list_source, &src->sinks);
201 path->name = dest->kcontrols[i].name;
202 dapm_set_path_status(dest, path, i);
203 return 0;
204 }
205 }
206 return -ENODEV;
207}
208
209/* update dapm codec register bits */
210static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
211{
212 int change, power;
213 unsigned short old, new;
214 struct snd_soc_codec *codec = widget->codec;
215
216 /* check for valid widgets */
217 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
218 widget->id == snd_soc_dapm_output ||
219 widget->id == snd_soc_dapm_hp ||
220 widget->id == snd_soc_dapm_mic ||
221 widget->id == snd_soc_dapm_line ||
222 widget->id == snd_soc_dapm_spk)
223 return 0;
224
225 power = widget->power;
226 if (widget->invert)
227 power = (power ? 0:1);
228
229 old = snd_soc_read(codec, widget->reg);
230 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
231
232 change = old != new;
233 if (change) {
234 pop_dbg("pop test %s : %s in %d ms\n", widget->name,
Mark Brown15e4c722008-07-02 11:51:20 +0100235 widget->power ? "on" : "off", pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200236 snd_soc_write(codec, widget->reg, new);
Mark Brown15e4c722008-07-02 11:51:20 +0100237 pop_wait();
Richard Purdie2b97eab2006-10-06 18:32:18 +0200238 }
Mark Brownc1286b82008-07-07 19:26:03 +0100239 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
240 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200241 return change;
242}
243
244/* ramps the volume up or down to minimise pops before or after a
245 * DAPM power event */
246static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
247{
248 const struct snd_kcontrol_new *k = widget->kcontrols;
249
250 if (widget->muted && !power)
251 return 0;
252 if (!widget->muted && power)
253 return 0;
254
255 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100256 struct soc_mixer_control *mc =
257 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400258 unsigned int reg = mc->reg;
259 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100260 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400261 unsigned int mask = (1 << fls(max)) - 1;
262 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200263
264 if (power) {
265 int i;
266 /* power up has happended, increase volume to last level */
267 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100268 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200269 snd_soc_update_bits(widget->codec, reg, mask, i);
270 } else {
271 for (i = 0; i < widget->saved_value; i++)
272 snd_soc_update_bits(widget->codec, reg, mask, i);
273 }
274 widget->muted = 0;
275 } else {
276 /* power down is about to occur, decrease volume to mute */
277 int val = snd_soc_read(widget->codec, reg);
278 int i = widget->saved_value = (val >> shift) & mask;
279 if (invert) {
280 for (; i < mask; i++)
281 snd_soc_update_bits(widget->codec, reg, mask, i);
282 } else {
283 for (; i > 0; i--)
284 snd_soc_update_bits(widget->codec, reg, mask, i);
285 }
286 widget->muted = 1;
287 }
288 }
289 return 0;
290}
291
292/* create new dapm mixer control */
293static int dapm_new_mixer(struct snd_soc_codec *codec,
294 struct snd_soc_dapm_widget *w)
295{
296 int i, ret = 0;
297 char name[32];
298 struct snd_soc_dapm_path *path;
299
300 /* add kcontrol */
301 for (i = 0; i < w->num_kcontrols; i++) {
302
303 /* match name */
304 list_for_each_entry(path, &w->sources, list_sink) {
305
306 /* mixer/mux paths name must match control name */
307 if (path->name != (char*)w->kcontrols[i].name)
308 continue;
309
310 /* add dapm control with long name */
311 snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
312 path->long_name = kstrdup (name, GFP_KERNEL);
313 if (path->long_name == NULL)
314 return -ENOMEM;
315
316 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
317 path->long_name);
318 ret = snd_ctl_add(codec->card, path->kcontrol);
319 if (ret < 0) {
320 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
321 path->long_name);
322 kfree(path->long_name);
323 path->long_name = NULL;
324 return ret;
325 }
326 }
327 }
328 return ret;
329}
330
331/* create new dapm mux control */
332static int dapm_new_mux(struct snd_soc_codec *codec,
333 struct snd_soc_dapm_widget *w)
334{
335 struct snd_soc_dapm_path *path = NULL;
336 struct snd_kcontrol *kcontrol;
337 int ret = 0;
338
339 if (!w->num_kcontrols) {
340 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
341 return -EINVAL;
342 }
343
344 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
345 ret = snd_ctl_add(codec->card, kcontrol);
346 if (ret < 0)
347 goto err;
348
349 list_for_each_entry(path, &w->sources, list_sink)
350 path->kcontrol = kcontrol;
351
352 return ret;
353
354err:
355 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
356 return ret;
357}
358
359/* create new dapm volume control */
360static int dapm_new_pga(struct snd_soc_codec *codec,
361 struct snd_soc_dapm_widget *w)
362{
363 struct snd_kcontrol *kcontrol;
364 int ret = 0;
365
366 if (!w->num_kcontrols)
367 return -EINVAL;
368
369 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
370 ret = snd_ctl_add(codec->card, kcontrol);
371 if (ret < 0) {
372 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
373 return ret;
374 }
375
376 return ret;
377}
378
379/* reset 'walked' bit for each dapm path */
380static inline void dapm_clear_walk(struct snd_soc_codec *codec)
381{
382 struct snd_soc_dapm_path *p;
383
384 list_for_each_entry(p, &codec->dapm_paths, list)
385 p->walked = 0;
386}
387
388/*
389 * Recursively check for a completed path to an active or physically connected
390 * output widget. Returns number of complete paths.
391 */
392static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
393{
394 struct snd_soc_dapm_path *path;
395 int con = 0;
396
397 if (widget->id == snd_soc_dapm_adc && widget->active)
398 return 1;
399
400 if (widget->connected) {
401 /* connected pin ? */
402 if (widget->id == snd_soc_dapm_output && !widget->ext)
403 return 1;
404
405 /* connected jack or spk ? */
406 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
407 widget->id == snd_soc_dapm_line)
408 return 1;
409 }
410
411 list_for_each_entry(path, &widget->sinks, list_source) {
412 if (path->walked)
413 continue;
414
415 if (path->sink && path->connect) {
416 path->walked = 1;
417 con += is_connected_output_ep(path->sink);
418 }
419 }
420
421 return con;
422}
423
424/*
425 * Recursively check for a completed path to an active or physically connected
426 * input widget. Returns number of complete paths.
427 */
428static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
429{
430 struct snd_soc_dapm_path *path;
431 int con = 0;
432
433 /* active stream ? */
434 if (widget->id == snd_soc_dapm_dac && widget->active)
435 return 1;
436
437 if (widget->connected) {
438 /* connected pin ? */
439 if (widget->id == snd_soc_dapm_input && !widget->ext)
440 return 1;
441
442 /* connected VMID/Bias for lower pops */
443 if (widget->id == snd_soc_dapm_vmid)
444 return 1;
445
446 /* connected jack ? */
447 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
448 return 1;
449 }
450
451 list_for_each_entry(path, &widget->sources, list_sink) {
452 if (path->walked)
453 continue;
454
455 if (path->source && path->connect) {
456 path->walked = 1;
457 con += is_connected_input_ep(path->source);
458 }
459 }
460
461 return con;
462}
463
464/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300465 * Handler for generic register modifier widget.
466 */
467int dapm_reg_event(struct snd_soc_dapm_widget *w,
468 struct snd_kcontrol *kcontrol, int event)
469{
470 unsigned int val;
471
472 if (SND_SOC_DAPM_EVENT_ON(event))
473 val = w->on_val;
474 else
475 val = w->off_val;
476
477 snd_soc_update_bits(w->codec, -(w->reg + 1),
478 w->mask << w->shift, val << w->shift);
479
480 return 0;
481}
Mark Brown11589412008-07-29 11:42:23 +0100482EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300483
484/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200485 * Scan each dapm widget for complete audio path.
486 * A complete path is a route that has valid endpoints i.e.:-
487 *
488 * o DAC to output pin.
489 * o Input Pin to ADC.
490 * o Input pin to Output pin (bypass, sidetone)
491 * o DAC to ADC (loopback).
492 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100493static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494{
495 struct snd_soc_dapm_widget *w;
496 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
497
498 /* do we have a sequenced stream event */
499 if (event == SND_SOC_DAPM_STREAM_START) {
500 c = ARRAY_SIZE(dapm_up_seq);
501 seq = dapm_up_seq;
502 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
503 c = ARRAY_SIZE(dapm_down_seq);
504 seq = dapm_down_seq;
505 }
506
507 for(i = 0; i < c; i++) {
508 list_for_each_entry(w, &codec->dapm_widgets, list) {
509
510 /* is widget in stream order */
511 if (seq && seq[i] && w->id != seq[i])
512 continue;
513
514 /* vmid - no action */
515 if (w->id == snd_soc_dapm_vmid)
516 continue;
517
518 /* active ADC */
519 if (w->id == snd_soc_dapm_adc && w->active) {
520 in = is_connected_input_ep(w);
521 dapm_clear_walk(w->codec);
522 w->power = (in != 0) ? 1 : 0;
523 dapm_update_bits(w);
524 continue;
525 }
526
527 /* active DAC */
528 if (w->id == snd_soc_dapm_dac && w->active) {
529 out = is_connected_output_ep(w);
530 dapm_clear_walk(w->codec);
531 w->power = (out != 0) ? 1 : 0;
532 dapm_update_bits(w);
533 continue;
534 }
535
Richard Purdie2b97eab2006-10-06 18:32:18 +0200536 /* pre and post event widgets */
537 if (w->id == snd_soc_dapm_pre) {
538 if (!w->event)
539 continue;
540
541 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100542 ret = w->event(w,
543 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544 if (ret < 0)
545 return ret;
546 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100547 ret = w->event(w,
548 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549 if (ret < 0)
550 return ret;
551 }
552 continue;
553 }
554 if (w->id == snd_soc_dapm_post) {
555 if (!w->event)
556 continue;
557
558 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100559 ret = w->event(w,
560 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200561 if (ret < 0)
562 return ret;
563 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100564 ret = w->event(w,
565 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200566 if (ret < 0)
567 return ret;
568 }
569 continue;
570 }
571
572 /* all other widgets */
573 in = is_connected_input_ep(w);
574 dapm_clear_walk(w->codec);
575 out = is_connected_output_ep(w);
576 dapm_clear_walk(w->codec);
577 power = (out != 0 && in != 0) ? 1 : 0;
578 power_change = (w->power == power) ? 0: 1;
579 w->power = power;
580
Mark Brown2927d6e2008-07-17 15:06:50 +0100581 if (!power_change)
582 continue;
583
Richard Purdie2b97eab2006-10-06 18:32:18 +0200584 /* call any power change event handlers */
Mark Brown2927d6e2008-07-17 15:06:50 +0100585 if (w->event)
586 pr_debug("power %s event for %s flags %x\n",
587 w->power ? "on" : "off",
588 w->name, w->event_flags);
589
590 /* power up pre event */
591 if (power && w->event &&
592 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
593 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
594 if (ret < 0)
595 return ret;
596 }
597
598 /* power down pre event */
599 if (!power && w->event &&
600 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
601 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
602 if (ret < 0)
603 return ret;
604 }
605
Mark Brown9dd8d812008-07-17 15:06:51 +0100606 /* Lower PGA volume to reduce pops */
607 if (w->id == snd_soc_dapm_pga && !power)
608 dapm_set_pga(w, power);
609
Mark Brown2927d6e2008-07-17 15:06:50 +0100610 dapm_update_bits(w);
611
Mark Brown9dd8d812008-07-17 15:06:51 +0100612 /* Raise PGA volume to reduce pops */
613 if (w->id == snd_soc_dapm_pga && power)
614 dapm_set_pga(w, power);
615
Mark Brown2927d6e2008-07-17 15:06:50 +0100616 /* power up post event */
617 if (power && w->event &&
618 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
619 ret = w->event(w,
620 NULL, SND_SOC_DAPM_POST_PMU);
621 if (ret < 0)
622 return ret;
623 }
624
625 /* power down post event */
626 if (!power && w->event &&
627 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
628 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
629 if (ret < 0)
630 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 }
632 }
633 }
634
635 return ret;
636}
637
Mark Brownc1286b82008-07-07 19:26:03 +0100638#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200639static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
640{
641 struct snd_soc_dapm_widget *w;
642 struct snd_soc_dapm_path *p = NULL;
643 int in, out;
644
645 printk("DAPM %s %s\n", codec->name, action);
646
647 list_for_each_entry(w, &codec->dapm_widgets, list) {
648
649 /* only display widgets that effect routing */
650 switch (w->id) {
651 case snd_soc_dapm_pre:
652 case snd_soc_dapm_post:
653 case snd_soc_dapm_vmid:
654 continue;
655 case snd_soc_dapm_mux:
656 case snd_soc_dapm_output:
657 case snd_soc_dapm_input:
658 case snd_soc_dapm_switch:
659 case snd_soc_dapm_hp:
660 case snd_soc_dapm_mic:
661 case snd_soc_dapm_spk:
662 case snd_soc_dapm_line:
663 case snd_soc_dapm_micbias:
664 case snd_soc_dapm_dac:
665 case snd_soc_dapm_adc:
666 case snd_soc_dapm_pga:
667 case snd_soc_dapm_mixer:
668 if (w->name) {
669 in = is_connected_input_ep(w);
670 dapm_clear_walk(w->codec);
671 out = is_connected_output_ep(w);
672 dapm_clear_walk(w->codec);
673 printk("%s: %s in %d out %d\n", w->name,
674 w->power ? "On":"Off",in, out);
675
676 list_for_each_entry(p, &w->sources, list_sink) {
677 if (p->connect)
678 printk(" in %s %s\n", p->name ? p->name : "static",
679 p->source->name);
680 }
681 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682 if (p->connect)
683 printk(" out %s %s\n", p->name ? p->name : "static",
684 p->sink->name);
685 }
686 }
687 break;
688 }
689 }
690}
691#endif
692
693/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100694static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
695 struct snd_kcontrol *kcontrol, int mask,
696 int val, struct soc_enum* e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697{
698 struct snd_soc_dapm_path *path;
699 int found = 0;
700
701 if (widget->id != snd_soc_dapm_mux)
702 return -ENODEV;
703
704 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
705 return 0;
706
707 /* find dapm widget path assoc with kcontrol */
708 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
709 if (path->kcontrol != kcontrol)
710 continue;
711
712 if (!path->name || ! e->texts[val])
713 continue;
714
715 found = 1;
716 /* we now need to match the string in the enum to the path */
717 if (!(strcmp(path->name, e->texts[val])))
718 path->connect = 1; /* new connection */
719 else
720 path->connect = 0; /* old connection must be powered down */
721 }
722
Mark Brown3fccd8b2008-07-07 19:26:04 +0100723 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100725 dump_dapm(widget->codec, "mux power update");
726 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727
728 return 0;
729}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200730
Milan plzik1b075e32008-01-10 14:39:46 +0100731/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100732static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
733 struct snd_kcontrol *kcontrol, int reg,
734 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735{
736 struct snd_soc_dapm_path *path;
737 int found = 0;
738
Milan plzik1b075e32008-01-10 14:39:46 +0100739 if (widget->id != snd_soc_dapm_mixer &&
740 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 return -ENODEV;
742
743 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
744 return 0;
745
746 /* find dapm widget path assoc with kcontrol */
747 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
748 if (path->kcontrol != kcontrol)
749 continue;
750
751 /* found, now check type */
752 found = 1;
753 if (val)
754 /* new connection */
755 path->connect = invert ? 0:1;
756 else
757 /* old connection must be powered down */
758 path->connect = invert ? 1:0;
759 break;
760 }
761
Mark Brown3fccd8b2008-07-07 19:26:04 +0100762 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200763 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100764 dump_dapm(widget->codec, "mixer power update");
765 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766
767 return 0;
768}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200769
770/* show dapm widget status in sys fs */
771static ssize_t dapm_widget_show(struct device *dev,
772 struct device_attribute *attr, char *buf)
773{
774 struct snd_soc_device *devdata = dev_get_drvdata(dev);
775 struct snd_soc_codec *codec = devdata->codec;
776 struct snd_soc_dapm_widget *w;
777 int count = 0;
778 char *state = "not set";
779
780 list_for_each_entry(w, &codec->dapm_widgets, list) {
781
782 /* only display widgets that burnm power */
783 switch (w->id) {
784 case snd_soc_dapm_hp:
785 case snd_soc_dapm_mic:
786 case snd_soc_dapm_spk:
787 case snd_soc_dapm_line:
788 case snd_soc_dapm_micbias:
789 case snd_soc_dapm_dac:
790 case snd_soc_dapm_adc:
791 case snd_soc_dapm_pga:
792 case snd_soc_dapm_mixer:
793 if (w->name)
794 count += sprintf(buf + count, "%s: %s\n",
795 w->name, w->power ? "On":"Off");
796 break;
797 default:
798 break;
799 }
800 }
801
Mark Brown0be98982008-05-19 12:31:28 +0200802 switch (codec->bias_level) {
803 case SND_SOC_BIAS_ON:
804 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200805 break;
Mark Brown0be98982008-05-19 12:31:28 +0200806 case SND_SOC_BIAS_PREPARE:
807 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200808 break;
Mark Brown0be98982008-05-19 12:31:28 +0200809 case SND_SOC_BIAS_STANDBY:
810 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 break;
Mark Brown0be98982008-05-19 12:31:28 +0200812 case SND_SOC_BIAS_OFF:
813 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814 break;
815 }
816 count += sprintf(buf + count, "PM State: %s\n", state);
817
818 return count;
819}
820
821static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
822
823int snd_soc_dapm_sys_add(struct device *dev)
824{
825 int ret = 0;
826
Mark Brownf0062a92008-08-28 12:46:24 +0100827 if (!dapm_status)
828 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200829
Mark Brownf0062a92008-08-28 12:46:24 +0100830 ret = device_create_file(dev, &dev_attr_dapm_widget);
831 if (ret != 0)
832 return ret;
Mark Brown15e4c722008-07-02 11:51:20 +0100833
Mark Brownf0062a92008-08-28 12:46:24 +0100834 asoc_debugfs = debugfs_create_dir("asoc", NULL);
835 if (!IS_ERR(asoc_debugfs))
836 debugfs_create_u32("dapm_pop_time", 0744, asoc_debugfs,
837 &pop_time);
838 else
839 asoc_debugfs = NULL;
840
841 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842}
843
844static void snd_soc_dapm_sys_remove(struct device *dev)
845{
Mark Brown15e4c722008-07-02 11:51:20 +0100846 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100848 }
Mark Brownf0062a92008-08-28 12:46:24 +0100849
850 if (asoc_debugfs)
851 debugfs_remove_recursive(asoc_debugfs);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200852}
853
854/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100855static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856{
857 struct snd_soc_dapm_widget *w, *next_w;
858 struct snd_soc_dapm_path *p, *next_p;
859
860 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
861 list_del(&w->list);
862 kfree(w);
863 }
864
865 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
866 list_del(&p->list);
867 kfree(p->long_name);
868 kfree(p);
869 }
870}
871
Liam Girdwooda5302182008-07-07 13:35:17 +0100872static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
873 char *pin, int status)
874{
875 struct snd_soc_dapm_widget *w;
876
877 list_for_each_entry(w, &codec->dapm_widgets, list) {
878 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100879 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100880 w->connected = status;
881 return 0;
882 }
883 }
884
Mark Brownc1286b82008-07-07 19:26:03 +0100885 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100886 return -EINVAL;
887}
888
Richard Purdie2b97eab2006-10-06 18:32:18 +0200889/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100890 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200891 * @codec: audio codec
892 *
893 * Walks all dapm audio paths and powers widgets according to their
894 * stream or path usage.
895 *
896 * Returns 0 for success.
897 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100898int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200899{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100900 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
901 dump_dapm(codec, "sync");
902 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200903}
Liam Girdwooda5302182008-07-07 13:35:17 +0100904EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200905
Mark Brown105f1c22008-05-13 14:52:19 +0200906static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
907 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200908{
909 struct snd_soc_dapm_path *path;
910 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
911 int ret = 0;
912
913 /* find src and dest widgets */
914 list_for_each_entry(w, &codec->dapm_widgets, list) {
915
916 if (!wsink && !(strcmp(w->name, sink))) {
917 wsink = w;
918 continue;
919 }
920 if (!wsource && !(strcmp(w->name, source))) {
921 wsource = w;
922 }
923 }
924
925 if (wsource == NULL || wsink == NULL)
926 return -ENODEV;
927
928 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
929 if (!path)
930 return -ENOMEM;
931
932 path->source = wsource;
933 path->sink = wsink;
934 INIT_LIST_HEAD(&path->list);
935 INIT_LIST_HEAD(&path->list_source);
936 INIT_LIST_HEAD(&path->list_sink);
937
938 /* check for external widgets */
939 if (wsink->id == snd_soc_dapm_input) {
940 if (wsource->id == snd_soc_dapm_micbias ||
941 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +0200942 wsink->id == snd_soc_dapm_line ||
943 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200944 wsink->ext = 1;
945 }
946 if (wsource->id == snd_soc_dapm_output) {
947 if (wsink->id == snd_soc_dapm_spk ||
948 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +0200949 wsink->id == snd_soc_dapm_line ||
950 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200951 wsource->ext = 1;
952 }
953
954 /* connect static paths */
955 if (control == NULL) {
956 list_add(&path->list, &codec->dapm_paths);
957 list_add(&path->list_sink, &wsink->sources);
958 list_add(&path->list_source, &wsource->sinks);
959 path->connect = 1;
960 return 0;
961 }
962
963 /* connect dynamic paths */
964 switch(wsink->id) {
965 case snd_soc_dapm_adc:
966 case snd_soc_dapm_dac:
967 case snd_soc_dapm_pga:
968 case snd_soc_dapm_input:
969 case snd_soc_dapm_output:
970 case snd_soc_dapm_micbias:
971 case snd_soc_dapm_vmid:
972 case snd_soc_dapm_pre:
973 case snd_soc_dapm_post:
974 list_add(&path->list, &codec->dapm_paths);
975 list_add(&path->list_sink, &wsink->sources);
976 list_add(&path->list_source, &wsource->sinks);
977 path->connect = 1;
978 return 0;
979 case snd_soc_dapm_mux:
980 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
981 &wsink->kcontrols[0]);
982 if (ret != 0)
983 goto err;
984 break;
985 case snd_soc_dapm_switch:
986 case snd_soc_dapm_mixer:
987 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
988 if (ret != 0)
989 goto err;
990 break;
991 case snd_soc_dapm_hp:
992 case snd_soc_dapm_mic:
993 case snd_soc_dapm_line:
994 case snd_soc_dapm_spk:
995 list_add(&path->list, &codec->dapm_paths);
996 list_add(&path->list_sink, &wsink->sources);
997 list_add(&path->list_source, &wsource->sinks);
998 path->connect = 0;
999 return 0;
1000 }
1001 return 0;
1002
1003err:
1004 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1005 control, sink);
1006 kfree(path);
1007 return ret;
1008}
Mark Brown105f1c22008-05-13 14:52:19 +02001009
1010/**
1011 * snd_soc_dapm_connect_input - connect dapm widgets
1012 * @codec: audio codec
1013 * @sink: name of target widget
1014 * @control: mixer control name
1015 * @source: name of source name
1016 *
1017 * Connects 2 dapm widgets together via a named audio path. The sink is
1018 * the widget receiving the audio signal, whilst the source is the sender
1019 * of the audio signal.
1020 *
1021 * This function has been deprecated in favour of snd_soc_dapm_add_routes().
1022 *
1023 * Returns 0 for success else error.
1024 */
1025int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
1026 const char *control, const char *source)
1027{
1028 return snd_soc_dapm_add_route(codec, sink, control, source);
1029}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001030EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
1031
1032/**
Mark Brown105f1c22008-05-13 14:52:19 +02001033 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1034 * @codec: codec
1035 * @route: audio routes
1036 * @num: number of routes
1037 *
1038 * Connects 2 dapm widgets together via a named audio path. The sink is
1039 * the widget receiving the audio signal, whilst the source is the sender
1040 * of the audio signal.
1041 *
1042 * Returns 0 for success else error. On error all resources can be freed
1043 * with a call to snd_soc_card_free().
1044 */
1045int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1046 const struct snd_soc_dapm_route *route, int num)
1047{
1048 int i, ret;
1049
1050 for (i = 0; i < num; i++) {
1051 ret = snd_soc_dapm_add_route(codec, route->sink,
1052 route->control, route->source);
1053 if (ret < 0) {
1054 printk(KERN_ERR "Failed to add route %s->%s\n",
1055 route->source,
1056 route->sink);
1057 return ret;
1058 }
1059 route++;
1060 }
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1065
1066/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001067 * snd_soc_dapm_new_widgets - add new dapm widgets
1068 * @codec: audio codec
1069 *
1070 * Checks the codec for any new dapm widgets and creates them if found.
1071 *
1072 * Returns 0 for success.
1073 */
1074int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1075{
1076 struct snd_soc_dapm_widget *w;
1077
Richard Purdie2b97eab2006-10-06 18:32:18 +02001078 list_for_each_entry(w, &codec->dapm_widgets, list)
1079 {
1080 if (w->new)
1081 continue;
1082
1083 switch(w->id) {
1084 case snd_soc_dapm_switch:
1085 case snd_soc_dapm_mixer:
1086 dapm_new_mixer(codec, w);
1087 break;
1088 case snd_soc_dapm_mux:
1089 dapm_new_mux(codec, w);
1090 break;
1091 case snd_soc_dapm_adc:
1092 case snd_soc_dapm_dac:
1093 case snd_soc_dapm_pga:
1094 dapm_new_pga(codec, w);
1095 break;
1096 case snd_soc_dapm_input:
1097 case snd_soc_dapm_output:
1098 case snd_soc_dapm_micbias:
1099 case snd_soc_dapm_spk:
1100 case snd_soc_dapm_hp:
1101 case snd_soc_dapm_mic:
1102 case snd_soc_dapm_line:
1103 case snd_soc_dapm_vmid:
1104 case snd_soc_dapm_pre:
1105 case snd_soc_dapm_post:
1106 break;
1107 }
1108 w->new = 1;
1109 }
1110
1111 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001112 return 0;
1113}
1114EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1115
1116/**
1117 * snd_soc_dapm_get_volsw - dapm mixer get callback
1118 * @kcontrol: mixer control
1119 * @uinfo: control element information
1120 *
1121 * Callback to get the value of a dapm mixer control.
1122 *
1123 * Returns 0 for success.
1124 */
1125int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1126 struct snd_ctl_elem_value *ucontrol)
1127{
1128 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001129 struct soc_mixer_control *mc =
1130 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001131 unsigned int reg = mc->reg;
1132 unsigned int shift = mc->shift;
1133 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001134 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001135 unsigned int invert = mc->invert;
1136 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001137
1138 /* return the saved value if we are powered down */
1139 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1140 ucontrol->value.integer.value[0] = widget->saved_value;
1141 return 0;
1142 }
1143
1144 ucontrol->value.integer.value[0] =
1145 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1146 if (shift != rshift)
1147 ucontrol->value.integer.value[1] =
1148 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1149 if (invert) {
1150 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001151 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001152 if (shift != rshift)
1153 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001154 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001155 }
1156
1157 return 0;
1158}
1159EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1160
1161/**
1162 * snd_soc_dapm_put_volsw - dapm mixer set callback
1163 * @kcontrol: mixer control
1164 * @uinfo: control element information
1165 *
1166 * Callback to set the value of a dapm mixer control.
1167 *
1168 * Returns 0 for success.
1169 */
1170int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1171 struct snd_ctl_elem_value *ucontrol)
1172{
1173 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001174 struct soc_mixer_control *mc =
1175 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001176 unsigned int reg = mc->reg;
1177 unsigned int shift = mc->shift;
1178 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001179 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001180 unsigned int mask = (1 << fls(max)) - 1;
1181 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182 unsigned short val, val2, val_mask;
1183 int ret;
1184
1185 val = (ucontrol->value.integer.value[0] & mask);
1186
1187 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001188 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001189 val_mask = mask << shift;
1190 val = val << shift;
1191 if (shift != rshift) {
1192 val2 = (ucontrol->value.integer.value[1] & mask);
1193 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001194 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 val_mask |= mask << rshift;
1196 val |= val2 << rshift;
1197 }
1198
1199 mutex_lock(&widget->codec->mutex);
1200 widget->value = val;
1201
1202 /* save volume value if the widget is powered down */
1203 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1204 widget->saved_value = val;
1205 mutex_unlock(&widget->codec->mutex);
1206 return 1;
1207 }
1208
1209 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1210 if (widget->event) {
1211 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001212 ret = widget->event(widget, kcontrol,
1213 SND_SOC_DAPM_PRE_REG);
1214 if (ret < 0) {
1215 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001216 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001217 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218 }
1219 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1220 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001221 ret = widget->event(widget, kcontrol,
1222 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 } else
1224 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1225
1226out:
1227 mutex_unlock(&widget->codec->mutex);
1228 return ret;
1229}
1230EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1231
1232/**
1233 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1234 * @kcontrol: mixer control
1235 * @uinfo: control element information
1236 *
1237 * Callback to get the value of a dapm enumerated double mixer control.
1238 *
1239 * Returns 0 for success.
1240 */
1241int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1242 struct snd_ctl_elem_value *ucontrol)
1243{
1244 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1245 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1246 unsigned short val, bitmask;
1247
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001248 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249 ;
1250 val = snd_soc_read(widget->codec, e->reg);
1251 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1252 if (e->shift_l != e->shift_r)
1253 ucontrol->value.enumerated.item[1] =
1254 (val >> e->shift_r) & (bitmask - 1);
1255
1256 return 0;
1257}
1258EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1259
1260/**
1261 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1262 * @kcontrol: mixer control
1263 * @uinfo: control element information
1264 *
1265 * Callback to set the value of a dapm enumerated double mixer control.
1266 *
1267 * Returns 0 for success.
1268 */
1269int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1270 struct snd_ctl_elem_value *ucontrol)
1271{
1272 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1273 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1274 unsigned short val, mux;
1275 unsigned short mask, bitmask;
1276 int ret = 0;
1277
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001278 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001279 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001280 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001281 return -EINVAL;
1282 mux = ucontrol->value.enumerated.item[0];
1283 val = mux << e->shift_l;
1284 mask = (bitmask - 1) << e->shift_l;
1285 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001286 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287 return -EINVAL;
1288 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1289 mask |= (bitmask - 1) << e->shift_r;
1290 }
1291
1292 mutex_lock(&widget->codec->mutex);
1293 widget->value = val;
1294 dapm_mux_update_power(widget, kcontrol, mask, mux, e);
1295 if (widget->event) {
1296 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001297 ret = widget->event(widget,
1298 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299 if (ret < 0)
1300 goto out;
1301 }
1302 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1303 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001304 ret = widget->event(widget,
1305 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001306 } else
1307 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1308
1309out:
1310 mutex_unlock(&widget->codec->mutex);
1311 return ret;
1312}
1313EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1314
1315/**
1316 * snd_soc_dapm_new_control - create new dapm control
1317 * @codec: audio codec
1318 * @widget: widget template
1319 *
1320 * Creates a new dapm control based upon the template.
1321 *
1322 * Returns 0 for success else error.
1323 */
1324int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1325 const struct snd_soc_dapm_widget *widget)
1326{
1327 struct snd_soc_dapm_widget *w;
1328
1329 if ((w = dapm_cnew_widget(widget)) == NULL)
1330 return -ENOMEM;
1331
1332 w->codec = codec;
1333 INIT_LIST_HEAD(&w->sources);
1334 INIT_LIST_HEAD(&w->sinks);
1335 INIT_LIST_HEAD(&w->list);
1336 list_add(&w->list, &codec->dapm_widgets);
1337
1338 /* machine layer set ups unconnected pins and insertions */
1339 w->connected = 1;
1340 return 0;
1341}
1342EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1343
1344/**
Mark Brown4ba13272008-05-13 14:51:19 +02001345 * snd_soc_dapm_new_controls - create new dapm controls
1346 * @codec: audio codec
1347 * @widget: widget array
1348 * @num: number of widgets
1349 *
1350 * Creates new DAPM controls based upon the templates.
1351 *
1352 * Returns 0 for success else error.
1353 */
1354int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1355 const struct snd_soc_dapm_widget *widget,
1356 int num)
1357{
1358 int i, ret;
1359
1360 for (i = 0; i < num; i++) {
1361 ret = snd_soc_dapm_new_control(codec, widget);
1362 if (ret < 0)
1363 return ret;
1364 widget++;
1365 }
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1369
1370
1371/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001372 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1373 * @codec: audio codec
1374 * @stream: stream name
1375 * @event: stream event
1376 *
1377 * Sends a stream event to the dapm core. The core then makes any
1378 * necessary widget power changes.
1379 *
1380 * Returns 0 for success else error.
1381 */
1382int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1383 char *stream, int event)
1384{
1385 struct snd_soc_dapm_widget *w;
1386
Seth Forshee11da21a2007-02-02 17:14:19 +01001387 if (stream == NULL)
1388 return 0;
1389
Richard Purdie2b97eab2006-10-06 18:32:18 +02001390 mutex_lock(&codec->mutex);
1391 list_for_each_entry(w, &codec->dapm_widgets, list)
1392 {
1393 if (!w->sname)
1394 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001395 pr_debug("widget %s\n %s stream %s event %d\n",
1396 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001397 if (strstr(w->sname, stream)) {
1398 switch(event) {
1399 case SND_SOC_DAPM_STREAM_START:
1400 w->active = 1;
1401 break;
1402 case SND_SOC_DAPM_STREAM_STOP:
1403 w->active = 0;
1404 break;
1405 case SND_SOC_DAPM_STREAM_SUSPEND:
1406 if (w->active)
1407 w->suspend = 1;
1408 w->active = 0;
1409 break;
1410 case SND_SOC_DAPM_STREAM_RESUME:
1411 if (w->suspend) {
1412 w->active = 1;
1413 w->suspend = 0;
1414 }
1415 break;
1416 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1417 break;
1418 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1419 break;
1420 }
1421 }
1422 }
1423 mutex_unlock(&codec->mutex);
1424
1425 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001426 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001427 return 0;
1428}
1429EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1430
1431/**
Mark Brown0be98982008-05-19 12:31:28 +02001432 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001433 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001434 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001435 *
Mark Brown0be98982008-05-19 12:31:28 +02001436 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001437 *
1438 * Returns 0 for success else error.
1439 */
Mark Brown0be98982008-05-19 12:31:28 +02001440int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1441 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001442{
1443 struct snd_soc_codec *codec = socdev->codec;
1444 struct snd_soc_machine *machine = socdev->machine;
Mark Brown0be98982008-05-19 12:31:28 +02001445 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001446
Mark Brown0be98982008-05-19 12:31:28 +02001447 if (machine->set_bias_level)
1448 ret = machine->set_bias_level(machine, level);
1449 if (ret == 0 && codec->set_bias_level)
1450 ret = codec->set_bias_level(codec, level);
1451
1452 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001453}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001454
1455/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001456 * snd_soc_dapm_enable_pin - enable pin.
1457 * @snd_soc_codec: SoC codec
1458 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001459 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001460 * Enables input/output pin and it's parents or children widgets iff there is
1461 * a valid audio route and active audio stream.
1462 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1463 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001464 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001465int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001466{
Liam Girdwooda5302182008-07-07 13:35:17 +01001467 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468}
Liam Girdwooda5302182008-07-07 13:35:17 +01001469EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470
1471/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001472 * snd_soc_dapm_disable_pin - disable pin.
1473 * @codec: SoC codec
1474 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001475 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001476 * Disables input/output pin and it's parents or children widgets.
1477 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1478 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001479 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001480int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1481{
1482 return snd_soc_dapm_set_pin(codec, pin, 0);
1483}
1484EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1485
1486/**
1487 * snd_soc_dapm_get_pin_status - get audio pin status
1488 * @codec: audio codec
1489 * @pin: audio signal pin endpoint (or start point)
1490 *
1491 * Get audio pin status - connected or disconnected.
1492 *
1493 * Returns 1 for connected otherwise 0.
1494 */
1495int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001496{
1497 struct snd_soc_dapm_widget *w;
1498
1499 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001500 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001501 return w->connected;
1502 }
1503
1504 return 0;
1505}
Liam Girdwooda5302182008-07-07 13:35:17 +01001506EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001507
1508/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509 * snd_soc_dapm_free - free dapm resources
1510 * @socdev: SoC device
1511 *
1512 * Free all dapm widgets and resources.
1513 */
1514void snd_soc_dapm_free(struct snd_soc_device *socdev)
1515{
1516 struct snd_soc_codec *codec = socdev->codec;
1517
1518 snd_soc_dapm_sys_remove(socdev->dev);
1519 dapm_free_widgets(codec);
1520}
1521EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1522
1523/* Module information */
1524MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1525MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1526MODULE_LICENSE("GPL");