blob: 407092c226f9c070749987115d5f92ad6c517916 [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,
56 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
57 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
58};
59static int dapm_down_seq[] = {
60 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
61 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
62 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
63};
64
65static int dapm_status = 1;
66module_param(dapm_status, int, 0);
67MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
68
Troy Kisky12ef1932008-10-13 17:42:14 -070069static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010070{
71 if (pop_time)
72 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
73}
74
Troy Kisky12ef1932008-10-13 17:42:14 -070075static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010076{
77 va_list args;
78
79 va_start(args, fmt);
80
81 if (pop_time) {
82 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070083 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010084 }
85
86 va_end(args);
87}
88
Richard Purdie2b97eab2006-10-06 18:32:18 +020089/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010090static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020091 const struct snd_soc_dapm_widget *_widget)
92{
Takashi Iwai88cb4292007-02-05 14:56:20 +010093 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020094}
95
96/* set up initial codec paths */
97static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
98 struct snd_soc_dapm_path *p, int i)
99{
100 switch (w->id) {
101 case snd_soc_dapm_switch:
102 case snd_soc_dapm_mixer: {
103 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100104 struct soc_mixer_control *mc = (struct soc_mixer_control *)
105 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400106 unsigned int reg = mc->reg;
107 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100108 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400109 unsigned int mask = (1 << fls(max)) - 1;
110 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200111
112 val = snd_soc_read(w->codec, reg);
113 val = (val >> shift) & mask;
114
115 if ((invert && !val) || (!invert && val))
116 p->connect = 1;
117 else
118 p->connect = 0;
119 }
120 break;
121 case snd_soc_dapm_mux: {
122 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
123 int val, item, bitmask;
124
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100125 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200126 ;
127 val = snd_soc_read(w->codec, e->reg);
128 item = (val >> e->shift_l) & (bitmask - 1);
129
130 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100131 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200132 if (!(strcmp(p->name, e->texts[i])) && item == i)
133 p->connect = 1;
134 }
135 }
136 break;
137 /* does not effect routing - always connected */
138 case snd_soc_dapm_pga:
139 case snd_soc_dapm_output:
140 case snd_soc_dapm_adc:
141 case snd_soc_dapm_input:
142 case snd_soc_dapm_dac:
143 case snd_soc_dapm_micbias:
144 case snd_soc_dapm_vmid:
145 p->connect = 1;
146 break;
147 /* does effect routing - dynamically connected */
148 case snd_soc_dapm_hp:
149 case snd_soc_dapm_mic:
150 case snd_soc_dapm_spk:
151 case snd_soc_dapm_line:
152 case snd_soc_dapm_pre:
153 case snd_soc_dapm_post:
154 p->connect = 0;
155 break;
156 }
157}
158
159/* connect mux widget to it's interconnecting audio paths */
160static int dapm_connect_mux(struct snd_soc_codec *codec,
161 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
162 struct snd_soc_dapm_path *path, const char *control_name,
163 const struct snd_kcontrol_new *kcontrol)
164{
165 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
166 int i;
167
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100168 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200169 if (!(strcmp(control_name, e->texts[i]))) {
170 list_add(&path->list, &codec->dapm_paths);
171 list_add(&path->list_sink, &dest->sources);
172 list_add(&path->list_source, &src->sinks);
173 path->name = (char*)e->texts[i];
174 dapm_set_path_status(dest, path, 0);
175 return 0;
176 }
177 }
178
179 return -ENODEV;
180}
181
182/* connect mixer widget to it's interconnecting audio paths */
183static int dapm_connect_mixer(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{
187 int i;
188
189 /* search for mixer kcontrol */
190 for (i = 0; i < dest->num_kcontrols; i++) {
191 if (!strcmp(control_name, dest->kcontrols[i].name)) {
192 list_add(&path->list, &codec->dapm_paths);
193 list_add(&path->list_sink, &dest->sources);
194 list_add(&path->list_source, &src->sinks);
195 path->name = dest->kcontrols[i].name;
196 dapm_set_path_status(dest, path, i);
197 return 0;
198 }
199 }
200 return -ENODEV;
201}
202
203/* update dapm codec register bits */
204static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
205{
206 int change, power;
207 unsigned short old, new;
208 struct snd_soc_codec *codec = widget->codec;
209
210 /* check for valid widgets */
211 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
212 widget->id == snd_soc_dapm_output ||
213 widget->id == snd_soc_dapm_hp ||
214 widget->id == snd_soc_dapm_mic ||
215 widget->id == snd_soc_dapm_line ||
216 widget->id == snd_soc_dapm_spk)
217 return 0;
218
219 power = widget->power;
220 if (widget->invert)
221 power = (power ? 0:1);
222
223 old = snd_soc_read(codec, widget->reg);
224 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
225
226 change = old != new;
227 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700228 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
229 widget->name, widget->power ? "on" : "off",
230 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200231 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700232 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200233 }
Mark Brownc1286b82008-07-07 19:26:03 +0100234 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
235 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200236 return change;
237}
238
239/* ramps the volume up or down to minimise pops before or after a
240 * DAPM power event */
241static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
242{
243 const struct snd_kcontrol_new *k = widget->kcontrols;
244
245 if (widget->muted && !power)
246 return 0;
247 if (!widget->muted && power)
248 return 0;
249
250 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100251 struct soc_mixer_control *mc =
252 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400253 unsigned int reg = mc->reg;
254 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100255 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400256 unsigned int mask = (1 << fls(max)) - 1;
257 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258
259 if (power) {
260 int i;
261 /* power up has happended, increase volume to last level */
262 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100263 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200264 snd_soc_update_bits(widget->codec, reg, mask, i);
265 } else {
266 for (i = 0; i < widget->saved_value; i++)
267 snd_soc_update_bits(widget->codec, reg, mask, i);
268 }
269 widget->muted = 0;
270 } else {
271 /* power down is about to occur, decrease volume to mute */
272 int val = snd_soc_read(widget->codec, reg);
273 int i = widget->saved_value = (val >> shift) & mask;
274 if (invert) {
275 for (; i < mask; i++)
276 snd_soc_update_bits(widget->codec, reg, mask, i);
277 } else {
278 for (; i > 0; i--)
279 snd_soc_update_bits(widget->codec, reg, mask, i);
280 }
281 widget->muted = 1;
282 }
283 }
284 return 0;
285}
286
287/* create new dapm mixer control */
288static int dapm_new_mixer(struct snd_soc_codec *codec,
289 struct snd_soc_dapm_widget *w)
290{
291 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000292 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 struct snd_soc_dapm_path *path;
294
295 /* add kcontrol */
296 for (i = 0; i < w->num_kcontrols; i++) {
297
298 /* match name */
299 list_for_each_entry(path, &w->sources, list_sink) {
300
301 /* mixer/mux paths name must match control name */
302 if (path->name != (char*)w->kcontrols[i].name)
303 continue;
304
305 /* add dapm control with long name */
Mark Brown219b93f2008-10-28 13:02:31 +0000306 name_len = 2 + strlen(w->name)
307 + strlen(w->kcontrols[i].name);
308 path->long_name = kmalloc(name_len, GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200309 if (path->long_name == NULL)
310 return -ENOMEM;
311
Mark Brown219b93f2008-10-28 13:02:31 +0000312 snprintf(path->long_name, name_len, "%s %s",
313 w->name, w->kcontrols[i].name);
314 path->long_name[name_len - 1] = '\0';
315
Richard Purdie2b97eab2006-10-06 18:32:18 +0200316 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,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800696 int mux, 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
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800712 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 continue;
714
715 found = 1;
716 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800717 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718 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{
Mark Brownf0062a92008-08-28 12:46:24 +0100825 if (!dapm_status)
826 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827
Mark Brownf0062a92008-08-28 12:46:24 +0100828 ret = device_create_file(dev, &dev_attr_dapm_widget);
829 if (ret != 0)
830 return ret;
Troy Kisky12ef1932008-10-13 17:42:14 -0700831 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200832}
833
834static void snd_soc_dapm_sys_remove(struct device *dev)
835{
Mark Brown15e4c722008-07-02 11:51:20 +0100836 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100838 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839}
840
841/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100842static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200843{
844 struct snd_soc_dapm_widget *w, *next_w;
845 struct snd_soc_dapm_path *p, *next_p;
846
847 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
848 list_del(&w->list);
849 kfree(w);
850 }
851
852 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
853 list_del(&p->list);
854 kfree(p->long_name);
855 kfree(p);
856 }
857}
858
Liam Girdwooda5302182008-07-07 13:35:17 +0100859static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
860 char *pin, int status)
861{
862 struct snd_soc_dapm_widget *w;
863
864 list_for_each_entry(w, &codec->dapm_widgets, list) {
865 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100866 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100867 w->connected = status;
868 return 0;
869 }
870 }
871
Mark Brownc1286b82008-07-07 19:26:03 +0100872 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100873 return -EINVAL;
874}
875
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100877 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200878 * @codec: audio codec
879 *
880 * Walks all dapm audio paths and powers widgets according to their
881 * stream or path usage.
882 *
883 * Returns 0 for success.
884 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100885int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200886{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100887 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
888 dump_dapm(codec, "sync");
889 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200890}
Liam Girdwooda5302182008-07-07 13:35:17 +0100891EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200892
Mark Brown105f1c22008-05-13 14:52:19 +0200893static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
894 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895{
896 struct snd_soc_dapm_path *path;
897 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
898 int ret = 0;
899
900 /* find src and dest widgets */
901 list_for_each_entry(w, &codec->dapm_widgets, list) {
902
903 if (!wsink && !(strcmp(w->name, sink))) {
904 wsink = w;
905 continue;
906 }
907 if (!wsource && !(strcmp(w->name, source))) {
908 wsource = w;
909 }
910 }
911
912 if (wsource == NULL || wsink == NULL)
913 return -ENODEV;
914
915 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
916 if (!path)
917 return -ENOMEM;
918
919 path->source = wsource;
920 path->sink = wsink;
921 INIT_LIST_HEAD(&path->list);
922 INIT_LIST_HEAD(&path->list_source);
923 INIT_LIST_HEAD(&path->list_sink);
924
925 /* check for external widgets */
926 if (wsink->id == snd_soc_dapm_input) {
927 if (wsource->id == snd_soc_dapm_micbias ||
928 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +0200929 wsink->id == snd_soc_dapm_line ||
930 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931 wsink->ext = 1;
932 }
933 if (wsource->id == snd_soc_dapm_output) {
934 if (wsink->id == snd_soc_dapm_spk ||
935 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +0200936 wsink->id == snd_soc_dapm_line ||
937 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200938 wsource->ext = 1;
939 }
940
941 /* connect static paths */
942 if (control == NULL) {
943 list_add(&path->list, &codec->dapm_paths);
944 list_add(&path->list_sink, &wsink->sources);
945 list_add(&path->list_source, &wsource->sinks);
946 path->connect = 1;
947 return 0;
948 }
949
950 /* connect dynamic paths */
951 switch(wsink->id) {
952 case snd_soc_dapm_adc:
953 case snd_soc_dapm_dac:
954 case snd_soc_dapm_pga:
955 case snd_soc_dapm_input:
956 case snd_soc_dapm_output:
957 case snd_soc_dapm_micbias:
958 case snd_soc_dapm_vmid:
959 case snd_soc_dapm_pre:
960 case snd_soc_dapm_post:
961 list_add(&path->list, &codec->dapm_paths);
962 list_add(&path->list_sink, &wsink->sources);
963 list_add(&path->list_source, &wsource->sinks);
964 path->connect = 1;
965 return 0;
966 case snd_soc_dapm_mux:
967 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
968 &wsink->kcontrols[0]);
969 if (ret != 0)
970 goto err;
971 break;
972 case snd_soc_dapm_switch:
973 case snd_soc_dapm_mixer:
974 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
975 if (ret != 0)
976 goto err;
977 break;
978 case snd_soc_dapm_hp:
979 case snd_soc_dapm_mic:
980 case snd_soc_dapm_line:
981 case snd_soc_dapm_spk:
982 list_add(&path->list, &codec->dapm_paths);
983 list_add(&path->list_sink, &wsink->sources);
984 list_add(&path->list_source, &wsource->sinks);
985 path->connect = 0;
986 return 0;
987 }
988 return 0;
989
990err:
991 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
992 control, sink);
993 kfree(path);
994 return ret;
995}
Mark Brown105f1c22008-05-13 14:52:19 +0200996
997/**
Mark Brown105f1c22008-05-13 14:52:19 +0200998 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
999 * @codec: codec
1000 * @route: audio routes
1001 * @num: number of routes
1002 *
1003 * Connects 2 dapm widgets together via a named audio path. The sink is
1004 * the widget receiving the audio signal, whilst the source is the sender
1005 * of the audio signal.
1006 *
1007 * Returns 0 for success else error. On error all resources can be freed
1008 * with a call to snd_soc_card_free().
1009 */
1010int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1011 const struct snd_soc_dapm_route *route, int num)
1012{
1013 int i, ret;
1014
1015 for (i = 0; i < num; i++) {
1016 ret = snd_soc_dapm_add_route(codec, route->sink,
1017 route->control, route->source);
1018 if (ret < 0) {
1019 printk(KERN_ERR "Failed to add route %s->%s\n",
1020 route->source,
1021 route->sink);
1022 return ret;
1023 }
1024 route++;
1025 }
1026
1027 return 0;
1028}
1029EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1030
1031/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001032 * snd_soc_dapm_new_widgets - add new dapm widgets
1033 * @codec: audio codec
1034 *
1035 * Checks the codec for any new dapm widgets and creates them if found.
1036 *
1037 * Returns 0 for success.
1038 */
1039int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1040{
1041 struct snd_soc_dapm_widget *w;
1042
Richard Purdie2b97eab2006-10-06 18:32:18 +02001043 list_for_each_entry(w, &codec->dapm_widgets, list)
1044 {
1045 if (w->new)
1046 continue;
1047
1048 switch(w->id) {
1049 case snd_soc_dapm_switch:
1050 case snd_soc_dapm_mixer:
1051 dapm_new_mixer(codec, w);
1052 break;
1053 case snd_soc_dapm_mux:
1054 dapm_new_mux(codec, w);
1055 break;
1056 case snd_soc_dapm_adc:
1057 case snd_soc_dapm_dac:
1058 case snd_soc_dapm_pga:
1059 dapm_new_pga(codec, w);
1060 break;
1061 case snd_soc_dapm_input:
1062 case snd_soc_dapm_output:
1063 case snd_soc_dapm_micbias:
1064 case snd_soc_dapm_spk:
1065 case snd_soc_dapm_hp:
1066 case snd_soc_dapm_mic:
1067 case snd_soc_dapm_line:
1068 case snd_soc_dapm_vmid:
1069 case snd_soc_dapm_pre:
1070 case snd_soc_dapm_post:
1071 break;
1072 }
1073 w->new = 1;
1074 }
1075
1076 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001077 return 0;
1078}
1079EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1080
1081/**
1082 * snd_soc_dapm_get_volsw - dapm mixer get callback
1083 * @kcontrol: mixer control
1084 * @uinfo: control element information
1085 *
1086 * Callback to get the value of a dapm mixer control.
1087 *
1088 * Returns 0 for success.
1089 */
1090int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1091 struct snd_ctl_elem_value *ucontrol)
1092{
1093 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001094 struct soc_mixer_control *mc =
1095 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001096 unsigned int reg = mc->reg;
1097 unsigned int shift = mc->shift;
1098 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001099 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001100 unsigned int invert = mc->invert;
1101 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001102
1103 /* return the saved value if we are powered down */
1104 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1105 ucontrol->value.integer.value[0] = widget->saved_value;
1106 return 0;
1107 }
1108
1109 ucontrol->value.integer.value[0] =
1110 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1111 if (shift != rshift)
1112 ucontrol->value.integer.value[1] =
1113 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1114 if (invert) {
1115 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001116 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001117 if (shift != rshift)
1118 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001119 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001120 }
1121
1122 return 0;
1123}
1124EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1125
1126/**
1127 * snd_soc_dapm_put_volsw - dapm mixer set callback
1128 * @kcontrol: mixer control
1129 * @uinfo: control element information
1130 *
1131 * Callback to set the value of a dapm mixer control.
1132 *
1133 * Returns 0 for success.
1134 */
1135int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1136 struct snd_ctl_elem_value *ucontrol)
1137{
1138 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001139 struct soc_mixer_control *mc =
1140 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001141 unsigned int reg = mc->reg;
1142 unsigned int shift = mc->shift;
1143 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001144 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001145 unsigned int mask = (1 << fls(max)) - 1;
1146 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001147 unsigned short val, val2, val_mask;
1148 int ret;
1149
1150 val = (ucontrol->value.integer.value[0] & mask);
1151
1152 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001153 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154 val_mask = mask << shift;
1155 val = val << shift;
1156 if (shift != rshift) {
1157 val2 = (ucontrol->value.integer.value[1] & mask);
1158 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001159 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001160 val_mask |= mask << rshift;
1161 val |= val2 << rshift;
1162 }
1163
1164 mutex_lock(&widget->codec->mutex);
1165 widget->value = val;
1166
1167 /* save volume value if the widget is powered down */
1168 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1169 widget->saved_value = val;
1170 mutex_unlock(&widget->codec->mutex);
1171 return 1;
1172 }
1173
1174 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1175 if (widget->event) {
1176 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001177 ret = widget->event(widget, kcontrol,
1178 SND_SOC_DAPM_PRE_REG);
1179 if (ret < 0) {
1180 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001181 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001182 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 }
1184 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1185 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001186 ret = widget->event(widget, kcontrol,
1187 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001188 } else
1189 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1190
1191out:
1192 mutex_unlock(&widget->codec->mutex);
1193 return ret;
1194}
1195EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1196
1197/**
1198 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1199 * @kcontrol: mixer control
1200 * @uinfo: control element information
1201 *
1202 * Callback to get the value of a dapm enumerated double mixer control.
1203 *
1204 * Returns 0 for success.
1205 */
1206int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1207 struct snd_ctl_elem_value *ucontrol)
1208{
1209 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1210 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1211 unsigned short val, bitmask;
1212
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001213 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214 ;
1215 val = snd_soc_read(widget->codec, e->reg);
1216 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1217 if (e->shift_l != e->shift_r)
1218 ucontrol->value.enumerated.item[1] =
1219 (val >> e->shift_r) & (bitmask - 1);
1220
1221 return 0;
1222}
1223EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1224
1225/**
1226 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1227 * @kcontrol: mixer control
1228 * @uinfo: control element information
1229 *
1230 * Callback to set the value of a dapm enumerated double mixer control.
1231 *
1232 * Returns 0 for success.
1233 */
1234int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1235 struct snd_ctl_elem_value *ucontrol)
1236{
1237 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1238 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1239 unsigned short val, mux;
1240 unsigned short mask, bitmask;
1241 int ret = 0;
1242
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001243 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001244 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001245 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001246 return -EINVAL;
1247 mux = ucontrol->value.enumerated.item[0];
1248 val = mux << e->shift_l;
1249 mask = (bitmask - 1) << e->shift_l;
1250 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001251 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001252 return -EINVAL;
1253 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1254 mask |= (bitmask - 1) << e->shift_r;
1255 }
1256
1257 mutex_lock(&widget->codec->mutex);
1258 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001259 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 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,
1263 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001264 if (ret < 0)
1265 goto out;
1266 }
1267 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1268 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001269 ret = widget->event(widget,
1270 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001271 } else
1272 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1273
1274out:
1275 mutex_unlock(&widget->codec->mutex);
1276 return ret;
1277}
1278EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1279
1280/**
1281 * snd_soc_dapm_new_control - create new dapm control
1282 * @codec: audio codec
1283 * @widget: widget template
1284 *
1285 * Creates a new dapm control based upon the template.
1286 *
1287 * Returns 0 for success else error.
1288 */
1289int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1290 const struct snd_soc_dapm_widget *widget)
1291{
1292 struct snd_soc_dapm_widget *w;
1293
1294 if ((w = dapm_cnew_widget(widget)) == NULL)
1295 return -ENOMEM;
1296
1297 w->codec = codec;
1298 INIT_LIST_HEAD(&w->sources);
1299 INIT_LIST_HEAD(&w->sinks);
1300 INIT_LIST_HEAD(&w->list);
1301 list_add(&w->list, &codec->dapm_widgets);
1302
1303 /* machine layer set ups unconnected pins and insertions */
1304 w->connected = 1;
1305 return 0;
1306}
1307EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1308
1309/**
Mark Brown4ba13272008-05-13 14:51:19 +02001310 * snd_soc_dapm_new_controls - create new dapm controls
1311 * @codec: audio codec
1312 * @widget: widget array
1313 * @num: number of widgets
1314 *
1315 * Creates new DAPM controls based upon the templates.
1316 *
1317 * Returns 0 for success else error.
1318 */
1319int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1320 const struct snd_soc_dapm_widget *widget,
1321 int num)
1322{
1323 int i, ret;
1324
1325 for (i = 0; i < num; i++) {
1326 ret = snd_soc_dapm_new_control(codec, widget);
1327 if (ret < 0)
1328 return ret;
1329 widget++;
1330 }
1331 return 0;
1332}
1333EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1334
1335
1336/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001337 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1338 * @codec: audio codec
1339 * @stream: stream name
1340 * @event: stream event
1341 *
1342 * Sends a stream event to the dapm core. The core then makes any
1343 * necessary widget power changes.
1344 *
1345 * Returns 0 for success else error.
1346 */
1347int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1348 char *stream, int event)
1349{
1350 struct snd_soc_dapm_widget *w;
1351
Seth Forshee11da21a2007-02-02 17:14:19 +01001352 if (stream == NULL)
1353 return 0;
1354
Richard Purdie2b97eab2006-10-06 18:32:18 +02001355 mutex_lock(&codec->mutex);
1356 list_for_each_entry(w, &codec->dapm_widgets, list)
1357 {
1358 if (!w->sname)
1359 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001360 pr_debug("widget %s\n %s stream %s event %d\n",
1361 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001362 if (strstr(w->sname, stream)) {
1363 switch(event) {
1364 case SND_SOC_DAPM_STREAM_START:
1365 w->active = 1;
1366 break;
1367 case SND_SOC_DAPM_STREAM_STOP:
1368 w->active = 0;
1369 break;
1370 case SND_SOC_DAPM_STREAM_SUSPEND:
1371 if (w->active)
1372 w->suspend = 1;
1373 w->active = 0;
1374 break;
1375 case SND_SOC_DAPM_STREAM_RESUME:
1376 if (w->suspend) {
1377 w->active = 1;
1378 w->suspend = 0;
1379 }
1380 break;
1381 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1382 break;
1383 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1384 break;
1385 }
1386 }
1387 }
1388 mutex_unlock(&codec->mutex);
1389
1390 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001391 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001392 return 0;
1393}
1394EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1395
1396/**
Mark Brown0be98982008-05-19 12:31:28 +02001397 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001398 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001399 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001400 *
Mark Brown0be98982008-05-19 12:31:28 +02001401 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001402 *
1403 * Returns 0 for success else error.
1404 */
Mark Brown0be98982008-05-19 12:31:28 +02001405int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1406 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001407{
1408 struct snd_soc_codec *codec = socdev->codec;
1409 struct snd_soc_machine *machine = socdev->machine;
Mark Brown0be98982008-05-19 12:31:28 +02001410 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001411
Mark Brown0be98982008-05-19 12:31:28 +02001412 if (machine->set_bias_level)
1413 ret = machine->set_bias_level(machine, level);
1414 if (ret == 0 && codec->set_bias_level)
1415 ret = codec->set_bias_level(codec, level);
1416
1417 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001418}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001419
1420/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001421 * snd_soc_dapm_enable_pin - enable pin.
1422 * @snd_soc_codec: SoC codec
1423 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001424 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001425 * Enables input/output pin and it's parents or children widgets iff there is
1426 * a valid audio route and active audio stream.
1427 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1428 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001429 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001430int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001431{
Liam Girdwooda5302182008-07-07 13:35:17 +01001432 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001433}
Liam Girdwooda5302182008-07-07 13:35:17 +01001434EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435
1436/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001437 * snd_soc_dapm_disable_pin - disable pin.
1438 * @codec: SoC codec
1439 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001440 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001441 * Disables input/output pin and it's parents or children widgets.
1442 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1443 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001444 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001445int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1446{
1447 return snd_soc_dapm_set_pin(codec, pin, 0);
1448}
1449EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1450
1451/**
Mark Brown5817b522008-09-24 11:23:11 +01001452 * snd_soc_dapm_nc_pin - permanently disable pin.
1453 * @codec: SoC codec
1454 * @pin: pin name
1455 *
1456 * Marks the specified pin as being not connected, disabling it along
1457 * any parent or child widgets. At present this is identical to
1458 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1459 * additional things such as disabling controls which only affect
1460 * paths through the pin.
1461 *
1462 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1463 * do any widget power switching.
1464 */
1465int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
1466{
1467 return snd_soc_dapm_set_pin(codec, pin, 0);
1468}
1469EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1470
1471/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001472 * snd_soc_dapm_get_pin_status - get audio pin status
1473 * @codec: audio codec
1474 * @pin: audio signal pin endpoint (or start point)
1475 *
1476 * Get audio pin status - connected or disconnected.
1477 *
1478 * Returns 1 for connected otherwise 0.
1479 */
1480int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001481{
1482 struct snd_soc_dapm_widget *w;
1483
1484 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001485 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001486 return w->connected;
1487 }
1488
1489 return 0;
1490}
Liam Girdwooda5302182008-07-07 13:35:17 +01001491EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001492
1493/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001494 * snd_soc_dapm_free - free dapm resources
1495 * @socdev: SoC device
1496 *
1497 * Free all dapm widgets and resources.
1498 */
1499void snd_soc_dapm_free(struct snd_soc_device *socdev)
1500{
1501 struct snd_soc_codec *codec = socdev->codec;
1502
1503 snd_soc_dapm_sys_remove(socdev->dev);
1504 dapm_free_widgets(codec);
1505}
1506EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1507
1508/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001509MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001510MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1511MODULE_LICENSE("GPL");