blob: 820347c9ae4bbc5d5281e981e94f0cd27d2e28ae [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>
Richard Purdie2b97eab2006-10-06 18:32:18 +020041#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc-dapm.h>
45#include <sound/initval.h>
46
47/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010048#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020050#else
51#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020052#endif
53
Richard Purdie2b97eab2006-10-06 18:32:18 +020054/* dapm power sequences - make this per codec in the future */
55static int dapm_up_seq[] = {
56 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
57 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
58 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
59};
60static int dapm_down_seq[] = {
61 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
62 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
63 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
64};
65
66static int dapm_status = 1;
67module_param(dapm_status, int, 0);
68MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
69
Mark Brown15e4c722008-07-02 11:51:20 +010070static unsigned int pop_time;
71
72static void pop_wait(void)
73{
74 if (pop_time)
75 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
76}
77
78static void pop_dbg(const char *fmt, ...)
79{
80 va_list args;
81
82 va_start(args, fmt);
83
84 if (pop_time) {
85 vprintk(fmt, args);
86 pop_wait();
87 }
88
89 va_end(args);
90}
91
Richard Purdie2b97eab2006-10-06 18:32:18 +020092/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010093static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020094 const struct snd_soc_dapm_widget *_widget)
95{
Takashi Iwai88cb4292007-02-05 14:56:20 +010096 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020097}
98
99/* set up initial codec paths */
100static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
101 struct snd_soc_dapm_path *p, int i)
102{
103 switch (w->id) {
104 case snd_soc_dapm_switch:
105 case snd_soc_dapm_mixer: {
106 int val;
107 int reg = w->kcontrols[i].private_value & 0xff;
108 int shift = (w->kcontrols[i].private_value >> 8) & 0x0f;
109 int mask = (w->kcontrols[i].private_value >> 16) & 0xff;
110 int invert = (w->kcontrols[i].private_value >> 24) & 0x01;
111
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
125 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
126 ;
127 val = snd_soc_read(w->codec, e->reg);
128 item = (val >> e->shift_l) & (bitmask - 1);
129
130 p->connect = 0;
131 for (i = 0; i < e->mask; i++) {
132 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
168 for (i = 0; i < e->mask; i++) {
169 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) {
228 pop_dbg("pop test %s : %s in %d ms\n", widget->name,
Mark Brown15e4c722008-07-02 11:51:20 +0100229 widget->power ? "on" : "off", pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200230 snd_soc_write(codec, widget->reg, new);
Mark Brown15e4c722008-07-02 11:51:20 +0100231 pop_wait();
Richard Purdie2b97eab2006-10-06 18:32:18 +0200232 }
Mark Brownc1286b82008-07-07 19:26:03 +0100233 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
234 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200235 return change;
236}
237
238/* ramps the volume up or down to minimise pops before or after a
239 * DAPM power event */
240static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
241{
242 const struct snd_kcontrol_new *k = widget->kcontrols;
243
244 if (widget->muted && !power)
245 return 0;
246 if (!widget->muted && power)
247 return 0;
248
249 if (widget->num_kcontrols && k) {
250 int reg = k->private_value & 0xff;
251 int shift = (k->private_value >> 8) & 0x0f;
252 int mask = (k->private_value >> 16) & 0xff;
253 int invert = (k->private_value >> 24) & 0x01;
254
255 if (power) {
256 int i;
257 /* power up has happended, increase volume to last level */
258 if (invert) {
259 for (i = mask; i > widget->saved_value; i--)
260 snd_soc_update_bits(widget->codec, reg, mask, i);
261 } else {
262 for (i = 0; i < widget->saved_value; i++)
263 snd_soc_update_bits(widget->codec, reg, mask, i);
264 }
265 widget->muted = 0;
266 } else {
267 /* power down is about to occur, decrease volume to mute */
268 int val = snd_soc_read(widget->codec, reg);
269 int i = widget->saved_value = (val >> shift) & mask;
270 if (invert) {
271 for (; i < mask; i++)
272 snd_soc_update_bits(widget->codec, reg, mask, i);
273 } else {
274 for (; i > 0; i--)
275 snd_soc_update_bits(widget->codec, reg, mask, i);
276 }
277 widget->muted = 1;
278 }
279 }
280 return 0;
281}
282
283/* create new dapm mixer control */
284static int dapm_new_mixer(struct snd_soc_codec *codec,
285 struct snd_soc_dapm_widget *w)
286{
287 int i, ret = 0;
288 char name[32];
289 struct snd_soc_dapm_path *path;
290
291 /* add kcontrol */
292 for (i = 0; i < w->num_kcontrols; i++) {
293
294 /* match name */
295 list_for_each_entry(path, &w->sources, list_sink) {
296
297 /* mixer/mux paths name must match control name */
298 if (path->name != (char*)w->kcontrols[i].name)
299 continue;
300
301 /* add dapm control with long name */
302 snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
303 path->long_name = kstrdup (name, GFP_KERNEL);
304 if (path->long_name == NULL)
305 return -ENOMEM;
306
307 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
308 path->long_name);
309 ret = snd_ctl_add(codec->card, path->kcontrol);
310 if (ret < 0) {
311 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
312 path->long_name);
313 kfree(path->long_name);
314 path->long_name = NULL;
315 return ret;
316 }
317 }
318 }
319 return ret;
320}
321
322/* create new dapm mux control */
323static int dapm_new_mux(struct snd_soc_codec *codec,
324 struct snd_soc_dapm_widget *w)
325{
326 struct snd_soc_dapm_path *path = NULL;
327 struct snd_kcontrol *kcontrol;
328 int ret = 0;
329
330 if (!w->num_kcontrols) {
331 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
332 return -EINVAL;
333 }
334
335 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
336 ret = snd_ctl_add(codec->card, kcontrol);
337 if (ret < 0)
338 goto err;
339
340 list_for_each_entry(path, &w->sources, list_sink)
341 path->kcontrol = kcontrol;
342
343 return ret;
344
345err:
346 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
347 return ret;
348}
349
350/* create new dapm volume control */
351static int dapm_new_pga(struct snd_soc_codec *codec,
352 struct snd_soc_dapm_widget *w)
353{
354 struct snd_kcontrol *kcontrol;
355 int ret = 0;
356
357 if (!w->num_kcontrols)
358 return -EINVAL;
359
360 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
361 ret = snd_ctl_add(codec->card, kcontrol);
362 if (ret < 0) {
363 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
364 return ret;
365 }
366
367 return ret;
368}
369
370/* reset 'walked' bit for each dapm path */
371static inline void dapm_clear_walk(struct snd_soc_codec *codec)
372{
373 struct snd_soc_dapm_path *p;
374
375 list_for_each_entry(p, &codec->dapm_paths, list)
376 p->walked = 0;
377}
378
379/*
380 * Recursively check for a completed path to an active or physically connected
381 * output widget. Returns number of complete paths.
382 */
383static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
384{
385 struct snd_soc_dapm_path *path;
386 int con = 0;
387
388 if (widget->id == snd_soc_dapm_adc && widget->active)
389 return 1;
390
391 if (widget->connected) {
392 /* connected pin ? */
393 if (widget->id == snd_soc_dapm_output && !widget->ext)
394 return 1;
395
396 /* connected jack or spk ? */
397 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
398 widget->id == snd_soc_dapm_line)
399 return 1;
400 }
401
402 list_for_each_entry(path, &widget->sinks, list_source) {
403 if (path->walked)
404 continue;
405
406 if (path->sink && path->connect) {
407 path->walked = 1;
408 con += is_connected_output_ep(path->sink);
409 }
410 }
411
412 return con;
413}
414
415/*
416 * Recursively check for a completed path to an active or physically connected
417 * input widget. Returns number of complete paths.
418 */
419static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
420{
421 struct snd_soc_dapm_path *path;
422 int con = 0;
423
424 /* active stream ? */
425 if (widget->id == snd_soc_dapm_dac && widget->active)
426 return 1;
427
428 if (widget->connected) {
429 /* connected pin ? */
430 if (widget->id == snd_soc_dapm_input && !widget->ext)
431 return 1;
432
433 /* connected VMID/Bias for lower pops */
434 if (widget->id == snd_soc_dapm_vmid)
435 return 1;
436
437 /* connected jack ? */
438 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
439 return 1;
440 }
441
442 list_for_each_entry(path, &widget->sources, list_sink) {
443 if (path->walked)
444 continue;
445
446 if (path->source && path->connect) {
447 path->walked = 1;
448 con += is_connected_input_ep(path->source);
449 }
450 }
451
452 return con;
453}
454
455/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300456 * Handler for generic register modifier widget.
457 */
458int dapm_reg_event(struct snd_soc_dapm_widget *w,
459 struct snd_kcontrol *kcontrol, int event)
460{
461 unsigned int val;
462
463 if (SND_SOC_DAPM_EVENT_ON(event))
464 val = w->on_val;
465 else
466 val = w->off_val;
467
468 snd_soc_update_bits(w->codec, -(w->reg + 1),
469 w->mask << w->shift, val << w->shift);
470
471 return 0;
472}
473
474/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 * Scan each dapm widget for complete audio path.
476 * A complete path is a route that has valid endpoints i.e.:-
477 *
478 * o DAC to output pin.
479 * o Input Pin to ADC.
480 * o Input pin to Output pin (bypass, sidetone)
481 * o DAC to ADC (loopback).
482 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100483static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200484{
485 struct snd_soc_dapm_widget *w;
486 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
487
488 /* do we have a sequenced stream event */
489 if (event == SND_SOC_DAPM_STREAM_START) {
490 c = ARRAY_SIZE(dapm_up_seq);
491 seq = dapm_up_seq;
492 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
493 c = ARRAY_SIZE(dapm_down_seq);
494 seq = dapm_down_seq;
495 }
496
497 for(i = 0; i < c; i++) {
498 list_for_each_entry(w, &codec->dapm_widgets, list) {
499
500 /* is widget in stream order */
501 if (seq && seq[i] && w->id != seq[i])
502 continue;
503
504 /* vmid - no action */
505 if (w->id == snd_soc_dapm_vmid)
506 continue;
507
508 /* active ADC */
509 if (w->id == snd_soc_dapm_adc && w->active) {
510 in = is_connected_input_ep(w);
511 dapm_clear_walk(w->codec);
512 w->power = (in != 0) ? 1 : 0;
513 dapm_update_bits(w);
514 continue;
515 }
516
517 /* active DAC */
518 if (w->id == snd_soc_dapm_dac && w->active) {
519 out = is_connected_output_ep(w);
520 dapm_clear_walk(w->codec);
521 w->power = (out != 0) ? 1 : 0;
522 dapm_update_bits(w);
523 continue;
524 }
525
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 /* pre and post event widgets */
527 if (w->id == snd_soc_dapm_pre) {
528 if (!w->event)
529 continue;
530
531 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100532 ret = w->event(w,
533 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534 if (ret < 0)
535 return ret;
536 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100537 ret = w->event(w,
538 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 if (ret < 0)
540 return ret;
541 }
542 continue;
543 }
544 if (w->id == snd_soc_dapm_post) {
545 if (!w->event)
546 continue;
547
548 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100549 ret = w->event(w,
550 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200551 if (ret < 0)
552 return ret;
553 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100554 ret = w->event(w,
555 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200556 if (ret < 0)
557 return ret;
558 }
559 continue;
560 }
561
562 /* all other widgets */
563 in = is_connected_input_ep(w);
564 dapm_clear_walk(w->codec);
565 out = is_connected_output_ep(w);
566 dapm_clear_walk(w->codec);
567 power = (out != 0 && in != 0) ? 1 : 0;
568 power_change = (w->power == power) ? 0: 1;
569 w->power = power;
570
Mark Brown2927d6e2008-07-17 15:06:50 +0100571 if (!power_change)
572 continue;
573
Richard Purdie2b97eab2006-10-06 18:32:18 +0200574 /* call any power change event handlers */
Mark Brown2927d6e2008-07-17 15:06:50 +0100575 if (w->event)
576 pr_debug("power %s event for %s flags %x\n",
577 w->power ? "on" : "off",
578 w->name, w->event_flags);
579
580 /* power up pre event */
581 if (power && w->event &&
582 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
583 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
584 if (ret < 0)
585 return ret;
586 }
587
588 /* power down pre event */
589 if (!power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
592 if (ret < 0)
593 return ret;
594 }
595
Mark Brown9dd8d812008-07-17 15:06:51 +0100596 /* Lower PGA volume to reduce pops */
597 if (w->id == snd_soc_dapm_pga && !power)
598 dapm_set_pga(w, power);
599
Mark Brown2927d6e2008-07-17 15:06:50 +0100600 dapm_update_bits(w);
601
Mark Brown9dd8d812008-07-17 15:06:51 +0100602 /* Raise PGA volume to reduce pops */
603 if (w->id == snd_soc_dapm_pga && power)
604 dapm_set_pga(w, power);
605
Mark Brown2927d6e2008-07-17 15:06:50 +0100606 /* power up post event */
607 if (power && w->event &&
608 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
609 ret = w->event(w,
610 NULL, SND_SOC_DAPM_POST_PMU);
611 if (ret < 0)
612 return ret;
613 }
614
615 /* power down post event */
616 if (!power && w->event &&
617 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
618 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
619 if (ret < 0)
620 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200621 }
622 }
623 }
624
625 return ret;
626}
627
Mark Brownc1286b82008-07-07 19:26:03 +0100628#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200629static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
630{
631 struct snd_soc_dapm_widget *w;
632 struct snd_soc_dapm_path *p = NULL;
633 int in, out;
634
635 printk("DAPM %s %s\n", codec->name, action);
636
637 list_for_each_entry(w, &codec->dapm_widgets, list) {
638
639 /* only display widgets that effect routing */
640 switch (w->id) {
641 case snd_soc_dapm_pre:
642 case snd_soc_dapm_post:
643 case snd_soc_dapm_vmid:
644 continue;
645 case snd_soc_dapm_mux:
646 case snd_soc_dapm_output:
647 case snd_soc_dapm_input:
648 case snd_soc_dapm_switch:
649 case snd_soc_dapm_hp:
650 case snd_soc_dapm_mic:
651 case snd_soc_dapm_spk:
652 case snd_soc_dapm_line:
653 case snd_soc_dapm_micbias:
654 case snd_soc_dapm_dac:
655 case snd_soc_dapm_adc:
656 case snd_soc_dapm_pga:
657 case snd_soc_dapm_mixer:
658 if (w->name) {
659 in = is_connected_input_ep(w);
660 dapm_clear_walk(w->codec);
661 out = is_connected_output_ep(w);
662 dapm_clear_walk(w->codec);
663 printk("%s: %s in %d out %d\n", w->name,
664 w->power ? "On":"Off",in, out);
665
666 list_for_each_entry(p, &w->sources, list_sink) {
667 if (p->connect)
668 printk(" in %s %s\n", p->name ? p->name : "static",
669 p->source->name);
670 }
671 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672 if (p->connect)
673 printk(" out %s %s\n", p->name ? p->name : "static",
674 p->sink->name);
675 }
676 }
677 break;
678 }
679 }
680}
681#endif
682
683/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100684static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
685 struct snd_kcontrol *kcontrol, int mask,
686 int val, struct soc_enum* e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687{
688 struct snd_soc_dapm_path *path;
689 int found = 0;
690
691 if (widget->id != snd_soc_dapm_mux)
692 return -ENODEV;
693
694 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
695 return 0;
696
697 /* find dapm widget path assoc with kcontrol */
698 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
699 if (path->kcontrol != kcontrol)
700 continue;
701
702 if (!path->name || ! e->texts[val])
703 continue;
704
705 found = 1;
706 /* we now need to match the string in the enum to the path */
707 if (!(strcmp(path->name, e->texts[val])))
708 path->connect = 1; /* new connection */
709 else
710 path->connect = 0; /* old connection must be powered down */
711 }
712
Mark Brown3fccd8b2008-07-07 19:26:04 +0100713 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200714 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100715 dump_dapm(widget->codec, "mux power update");
716 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717
718 return 0;
719}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720
Milan plzik1b075e32008-01-10 14:39:46 +0100721/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100722static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
723 struct snd_kcontrol *kcontrol, int reg,
724 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725{
726 struct snd_soc_dapm_path *path;
727 int found = 0;
728
Milan plzik1b075e32008-01-10 14:39:46 +0100729 if (widget->id != snd_soc_dapm_mixer &&
730 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731 return -ENODEV;
732
733 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
734 return 0;
735
736 /* find dapm widget path assoc with kcontrol */
737 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
738 if (path->kcontrol != kcontrol)
739 continue;
740
741 /* found, now check type */
742 found = 1;
743 if (val)
744 /* new connection */
745 path->connect = invert ? 0:1;
746 else
747 /* old connection must be powered down */
748 path->connect = invert ? 1:0;
749 break;
750 }
751
Mark Brown3fccd8b2008-07-07 19:26:04 +0100752 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100754 dump_dapm(widget->codec, "mixer power update");
755 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200756
757 return 0;
758}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200759
760/* show dapm widget status in sys fs */
761static ssize_t dapm_widget_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
764 struct snd_soc_device *devdata = dev_get_drvdata(dev);
765 struct snd_soc_codec *codec = devdata->codec;
766 struct snd_soc_dapm_widget *w;
767 int count = 0;
768 char *state = "not set";
769
770 list_for_each_entry(w, &codec->dapm_widgets, list) {
771
772 /* only display widgets that burnm power */
773 switch (w->id) {
774 case snd_soc_dapm_hp:
775 case snd_soc_dapm_mic:
776 case snd_soc_dapm_spk:
777 case snd_soc_dapm_line:
778 case snd_soc_dapm_micbias:
779 case snd_soc_dapm_dac:
780 case snd_soc_dapm_adc:
781 case snd_soc_dapm_pga:
782 case snd_soc_dapm_mixer:
783 if (w->name)
784 count += sprintf(buf + count, "%s: %s\n",
785 w->name, w->power ? "On":"Off");
786 break;
787 default:
788 break;
789 }
790 }
791
Mark Brown0be98982008-05-19 12:31:28 +0200792 switch (codec->bias_level) {
793 case SND_SOC_BIAS_ON:
794 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200795 break;
Mark Brown0be98982008-05-19 12:31:28 +0200796 case SND_SOC_BIAS_PREPARE:
797 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798 break;
Mark Brown0be98982008-05-19 12:31:28 +0200799 case SND_SOC_BIAS_STANDBY:
800 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801 break;
Mark Brown0be98982008-05-19 12:31:28 +0200802 case SND_SOC_BIAS_OFF:
803 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200804 break;
805 }
806 count += sprintf(buf + count, "PM State: %s\n", state);
807
808 return count;
809}
810
811static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
812
Mark Brown15e4c722008-07-02 11:51:20 +0100813/* pop/click delay times */
814static ssize_t dapm_pop_time_show(struct device *dev,
815 struct device_attribute *attr, char *buf)
816{
817 return sprintf(buf, "%d\n", pop_time);
818}
819
820static ssize_t dapm_pop_time_store(struct device *dev,
821 struct device_attribute *attr,
822 const char *buf, size_t count)
823
824{
Mark Brown73ead482008-07-04 16:01:14 +0100825 unsigned long val;
826
827 if (strict_strtoul(buf, 10, &val) >= 0)
828 pop_time = val;
829 else
Mark Brown15e4c722008-07-02 11:51:20 +0100830 printk(KERN_ERR "Unable to parse pop_time setting\n");
831
832 return count;
833}
834
835static DEVICE_ATTR(dapm_pop_time, 0744, dapm_pop_time_show,
836 dapm_pop_time_store);
837
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838int snd_soc_dapm_sys_add(struct device *dev)
839{
840 int ret = 0;
841
Mark Brown15e4c722008-07-02 11:51:20 +0100842 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200843 ret = device_create_file(dev, &dev_attr_dapm_widget);
844
Mark Brown15e4c722008-07-02 11:51:20 +0100845 if (ret == 0)
846 ret = device_create_file(dev, &dev_attr_dapm_pop_time);
847 }
848
Richard Purdie2b97eab2006-10-06 18:32:18 +0200849 return ret;
850}
851
852static void snd_soc_dapm_sys_remove(struct device *dev)
853{
Mark Brown15e4c722008-07-02 11:51:20 +0100854 if (dapm_status) {
855 device_remove_file(dev, &dev_attr_dapm_pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100857 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858}
859
860/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100861static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862{
863 struct snd_soc_dapm_widget *w, *next_w;
864 struct snd_soc_dapm_path *p, *next_p;
865
866 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
867 list_del(&w->list);
868 kfree(w);
869 }
870
871 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
872 list_del(&p->list);
873 kfree(p->long_name);
874 kfree(p);
875 }
876}
877
Liam Girdwooda5302182008-07-07 13:35:17 +0100878static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
879 char *pin, int status)
880{
881 struct snd_soc_dapm_widget *w;
882
883 list_for_each_entry(w, &codec->dapm_widgets, list) {
884 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100885 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100886 w->connected = status;
887 return 0;
888 }
889 }
890
Mark Brownc1286b82008-07-07 19:26:03 +0100891 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100892 return -EINVAL;
893}
894
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100896 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200897 * @codec: audio codec
898 *
899 * Walks all dapm audio paths and powers widgets according to their
900 * stream or path usage.
901 *
902 * Returns 0 for success.
903 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100904int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200905{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100906 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
907 dump_dapm(codec, "sync");
908 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200909}
Liam Girdwooda5302182008-07-07 13:35:17 +0100910EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200911
Mark Brown105f1c22008-05-13 14:52:19 +0200912static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
913 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200914{
915 struct snd_soc_dapm_path *path;
916 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
917 int ret = 0;
918
919 /* find src and dest widgets */
920 list_for_each_entry(w, &codec->dapm_widgets, list) {
921
922 if (!wsink && !(strcmp(w->name, sink))) {
923 wsink = w;
924 continue;
925 }
926 if (!wsource && !(strcmp(w->name, source))) {
927 wsource = w;
928 }
929 }
930
931 if (wsource == NULL || wsink == NULL)
932 return -ENODEV;
933
934 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
935 if (!path)
936 return -ENOMEM;
937
938 path->source = wsource;
939 path->sink = wsink;
940 INIT_LIST_HEAD(&path->list);
941 INIT_LIST_HEAD(&path->list_source);
942 INIT_LIST_HEAD(&path->list_sink);
943
944 /* check for external widgets */
945 if (wsink->id == snd_soc_dapm_input) {
946 if (wsource->id == snd_soc_dapm_micbias ||
947 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +0200948 wsink->id == snd_soc_dapm_line ||
949 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950 wsink->ext = 1;
951 }
952 if (wsource->id == snd_soc_dapm_output) {
953 if (wsink->id == snd_soc_dapm_spk ||
954 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +0200955 wsink->id == snd_soc_dapm_line ||
956 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957 wsource->ext = 1;
958 }
959
960 /* connect static paths */
961 if (control == NULL) {
962 list_add(&path->list, &codec->dapm_paths);
963 list_add(&path->list_sink, &wsink->sources);
964 list_add(&path->list_source, &wsource->sinks);
965 path->connect = 1;
966 return 0;
967 }
968
969 /* connect dynamic paths */
970 switch(wsink->id) {
971 case snd_soc_dapm_adc:
972 case snd_soc_dapm_dac:
973 case snd_soc_dapm_pga:
974 case snd_soc_dapm_input:
975 case snd_soc_dapm_output:
976 case snd_soc_dapm_micbias:
977 case snd_soc_dapm_vmid:
978 case snd_soc_dapm_pre:
979 case snd_soc_dapm_post:
980 list_add(&path->list, &codec->dapm_paths);
981 list_add(&path->list_sink, &wsink->sources);
982 list_add(&path->list_source, &wsource->sinks);
983 path->connect = 1;
984 return 0;
985 case snd_soc_dapm_mux:
986 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
987 &wsink->kcontrols[0]);
988 if (ret != 0)
989 goto err;
990 break;
991 case snd_soc_dapm_switch:
992 case snd_soc_dapm_mixer:
993 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
994 if (ret != 0)
995 goto err;
996 break;
997 case snd_soc_dapm_hp:
998 case snd_soc_dapm_mic:
999 case snd_soc_dapm_line:
1000 case snd_soc_dapm_spk:
1001 list_add(&path->list, &codec->dapm_paths);
1002 list_add(&path->list_sink, &wsink->sources);
1003 list_add(&path->list_source, &wsource->sinks);
1004 path->connect = 0;
1005 return 0;
1006 }
1007 return 0;
1008
1009err:
1010 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1011 control, sink);
1012 kfree(path);
1013 return ret;
1014}
Mark Brown105f1c22008-05-13 14:52:19 +02001015
1016/**
1017 * snd_soc_dapm_connect_input - connect dapm widgets
1018 * @codec: audio codec
1019 * @sink: name of target widget
1020 * @control: mixer control name
1021 * @source: name of source name
1022 *
1023 * Connects 2 dapm widgets together via a named audio path. The sink is
1024 * the widget receiving the audio signal, whilst the source is the sender
1025 * of the audio signal.
1026 *
1027 * This function has been deprecated in favour of snd_soc_dapm_add_routes().
1028 *
1029 * Returns 0 for success else error.
1030 */
1031int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
1032 const char *control, const char *source)
1033{
1034 return snd_soc_dapm_add_route(codec, sink, control, source);
1035}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001036EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
1037
1038/**
Mark Brown105f1c22008-05-13 14:52:19 +02001039 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1040 * @codec: codec
1041 * @route: audio routes
1042 * @num: number of routes
1043 *
1044 * Connects 2 dapm widgets together via a named audio path. The sink is
1045 * the widget receiving the audio signal, whilst the source is the sender
1046 * of the audio signal.
1047 *
1048 * Returns 0 for success else error. On error all resources can be freed
1049 * with a call to snd_soc_card_free().
1050 */
1051int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1052 const struct snd_soc_dapm_route *route, int num)
1053{
1054 int i, ret;
1055
1056 for (i = 0; i < num; i++) {
1057 ret = snd_soc_dapm_add_route(codec, route->sink,
1058 route->control, route->source);
1059 if (ret < 0) {
1060 printk(KERN_ERR "Failed to add route %s->%s\n",
1061 route->source,
1062 route->sink);
1063 return ret;
1064 }
1065 route++;
1066 }
1067
1068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1071
1072/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001073 * snd_soc_dapm_new_widgets - add new dapm widgets
1074 * @codec: audio codec
1075 *
1076 * Checks the codec for any new dapm widgets and creates them if found.
1077 *
1078 * Returns 0 for success.
1079 */
1080int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1081{
1082 struct snd_soc_dapm_widget *w;
1083
Richard Purdie2b97eab2006-10-06 18:32:18 +02001084 list_for_each_entry(w, &codec->dapm_widgets, list)
1085 {
1086 if (w->new)
1087 continue;
1088
1089 switch(w->id) {
1090 case snd_soc_dapm_switch:
1091 case snd_soc_dapm_mixer:
1092 dapm_new_mixer(codec, w);
1093 break;
1094 case snd_soc_dapm_mux:
1095 dapm_new_mux(codec, w);
1096 break;
1097 case snd_soc_dapm_adc:
1098 case snd_soc_dapm_dac:
1099 case snd_soc_dapm_pga:
1100 dapm_new_pga(codec, w);
1101 break;
1102 case snd_soc_dapm_input:
1103 case snd_soc_dapm_output:
1104 case snd_soc_dapm_micbias:
1105 case snd_soc_dapm_spk:
1106 case snd_soc_dapm_hp:
1107 case snd_soc_dapm_mic:
1108 case snd_soc_dapm_line:
1109 case snd_soc_dapm_vmid:
1110 case snd_soc_dapm_pre:
1111 case snd_soc_dapm_post:
1112 break;
1113 }
1114 w->new = 1;
1115 }
1116
1117 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1121
1122/**
1123 * snd_soc_dapm_get_volsw - dapm mixer get callback
1124 * @kcontrol: mixer control
1125 * @uinfo: control element information
1126 *
1127 * Callback to get the value of a dapm mixer control.
1128 *
1129 * Returns 0 for success.
1130 */
1131int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1132 struct snd_ctl_elem_value *ucontrol)
1133{
1134 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1135 int reg = kcontrol->private_value & 0xff;
1136 int shift = (kcontrol->private_value >> 8) & 0x0f;
1137 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001138 int max = (kcontrol->private_value >> 16) & 0xff;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139 int invert = (kcontrol->private_value >> 24) & 0x01;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001140 int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001141
1142 /* return the saved value if we are powered down */
1143 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1144 ucontrol->value.integer.value[0] = widget->saved_value;
1145 return 0;
1146 }
1147
1148 ucontrol->value.integer.value[0] =
1149 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1150 if (shift != rshift)
1151 ucontrol->value.integer.value[1] =
1152 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1153 if (invert) {
1154 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001155 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001156 if (shift != rshift)
1157 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001158 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001159 }
1160
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1164
1165/**
1166 * snd_soc_dapm_put_volsw - dapm mixer set callback
1167 * @kcontrol: mixer control
1168 * @uinfo: control element information
1169 *
1170 * Callback to set the value of a dapm mixer control.
1171 *
1172 * Returns 0 for success.
1173 */
1174int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1175 struct snd_ctl_elem_value *ucontrol)
1176{
1177 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1178 int reg = kcontrol->private_value & 0xff;
1179 int shift = (kcontrol->private_value >> 8) & 0x0f;
1180 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001181 int max = (kcontrol->private_value >> 16) & 0xff;
1182 int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 int invert = (kcontrol->private_value >> 24) & 0x01;
1184 unsigned short val, val2, val_mask;
1185 int ret;
1186
1187 val = (ucontrol->value.integer.value[0] & mask);
1188
1189 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001190 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001191 val_mask = mask << shift;
1192 val = val << shift;
1193 if (shift != rshift) {
1194 val2 = (ucontrol->value.integer.value[1] & mask);
1195 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001196 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001197 val_mask |= mask << rshift;
1198 val |= val2 << rshift;
1199 }
1200
1201 mutex_lock(&widget->codec->mutex);
1202 widget->value = val;
1203
1204 /* save volume value if the widget is powered down */
1205 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1206 widget->saved_value = val;
1207 mutex_unlock(&widget->codec->mutex);
1208 return 1;
1209 }
1210
1211 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1212 if (widget->event) {
1213 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001214 ret = widget->event(widget, kcontrol,
1215 SND_SOC_DAPM_PRE_REG);
1216 if (ret < 0) {
1217 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001219 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001220 }
1221 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1222 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001223 ret = widget->event(widget, kcontrol,
1224 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001225 } else
1226 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1227
1228out:
1229 mutex_unlock(&widget->codec->mutex);
1230 return ret;
1231}
1232EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1233
1234/**
1235 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1236 * @kcontrol: mixer control
1237 * @uinfo: control element information
1238 *
1239 * Callback to get the value of a dapm enumerated double mixer control.
1240 *
1241 * Returns 0 for success.
1242 */
1243int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1244 struct snd_ctl_elem_value *ucontrol)
1245{
1246 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1247 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1248 unsigned short val, bitmask;
1249
1250 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1251 ;
1252 val = snd_soc_read(widget->codec, e->reg);
1253 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1254 if (e->shift_l != e->shift_r)
1255 ucontrol->value.enumerated.item[1] =
1256 (val >> e->shift_r) & (bitmask - 1);
1257
1258 return 0;
1259}
1260EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1261
1262/**
1263 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1264 * @kcontrol: mixer control
1265 * @uinfo: control element information
1266 *
1267 * Callback to set the value of a dapm enumerated double mixer control.
1268 *
1269 * Returns 0 for success.
1270 */
1271int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1272 struct snd_ctl_elem_value *ucontrol)
1273{
1274 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1275 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1276 unsigned short val, mux;
1277 unsigned short mask, bitmask;
1278 int ret = 0;
1279
1280 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1281 ;
1282 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1283 return -EINVAL;
1284 mux = ucontrol->value.enumerated.item[0];
1285 val = mux << e->shift_l;
1286 mask = (bitmask - 1) << e->shift_l;
1287 if (e->shift_l != e->shift_r) {
1288 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1289 return -EINVAL;
1290 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1291 mask |= (bitmask - 1) << e->shift_r;
1292 }
1293
1294 mutex_lock(&widget->codec->mutex);
1295 widget->value = val;
1296 dapm_mux_update_power(widget, kcontrol, mask, mux, e);
1297 if (widget->event) {
1298 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001299 ret = widget->event(widget,
1300 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301 if (ret < 0)
1302 goto out;
1303 }
1304 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1305 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001306 ret = widget->event(widget,
1307 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001308 } else
1309 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1310
1311out:
1312 mutex_unlock(&widget->codec->mutex);
1313 return ret;
1314}
1315EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1316
1317/**
1318 * snd_soc_dapm_new_control - create new dapm control
1319 * @codec: audio codec
1320 * @widget: widget template
1321 *
1322 * Creates a new dapm control based upon the template.
1323 *
1324 * Returns 0 for success else error.
1325 */
1326int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1327 const struct snd_soc_dapm_widget *widget)
1328{
1329 struct snd_soc_dapm_widget *w;
1330
1331 if ((w = dapm_cnew_widget(widget)) == NULL)
1332 return -ENOMEM;
1333
1334 w->codec = codec;
1335 INIT_LIST_HEAD(&w->sources);
1336 INIT_LIST_HEAD(&w->sinks);
1337 INIT_LIST_HEAD(&w->list);
1338 list_add(&w->list, &codec->dapm_widgets);
1339
1340 /* machine layer set ups unconnected pins and insertions */
1341 w->connected = 1;
1342 return 0;
1343}
1344EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1345
1346/**
Mark Brown4ba13272008-05-13 14:51:19 +02001347 * snd_soc_dapm_new_controls - create new dapm controls
1348 * @codec: audio codec
1349 * @widget: widget array
1350 * @num: number of widgets
1351 *
1352 * Creates new DAPM controls based upon the templates.
1353 *
1354 * Returns 0 for success else error.
1355 */
1356int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1357 const struct snd_soc_dapm_widget *widget,
1358 int num)
1359{
1360 int i, ret;
1361
1362 for (i = 0; i < num; i++) {
1363 ret = snd_soc_dapm_new_control(codec, widget);
1364 if (ret < 0)
1365 return ret;
1366 widget++;
1367 }
1368 return 0;
1369}
1370EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1371
1372
1373/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001374 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1375 * @codec: audio codec
1376 * @stream: stream name
1377 * @event: stream event
1378 *
1379 * Sends a stream event to the dapm core. The core then makes any
1380 * necessary widget power changes.
1381 *
1382 * Returns 0 for success else error.
1383 */
1384int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1385 char *stream, int event)
1386{
1387 struct snd_soc_dapm_widget *w;
1388
Seth Forshee11da21a2007-02-02 17:14:19 +01001389 if (stream == NULL)
1390 return 0;
1391
Richard Purdie2b97eab2006-10-06 18:32:18 +02001392 mutex_lock(&codec->mutex);
1393 list_for_each_entry(w, &codec->dapm_widgets, list)
1394 {
1395 if (!w->sname)
1396 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001397 pr_debug("widget %s\n %s stream %s event %d\n",
1398 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001399 if (strstr(w->sname, stream)) {
1400 switch(event) {
1401 case SND_SOC_DAPM_STREAM_START:
1402 w->active = 1;
1403 break;
1404 case SND_SOC_DAPM_STREAM_STOP:
1405 w->active = 0;
1406 break;
1407 case SND_SOC_DAPM_STREAM_SUSPEND:
1408 if (w->active)
1409 w->suspend = 1;
1410 w->active = 0;
1411 break;
1412 case SND_SOC_DAPM_STREAM_RESUME:
1413 if (w->suspend) {
1414 w->active = 1;
1415 w->suspend = 0;
1416 }
1417 break;
1418 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1419 break;
1420 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1421 break;
1422 }
1423 }
1424 }
1425 mutex_unlock(&codec->mutex);
1426
1427 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001428 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001429 return 0;
1430}
1431EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1432
1433/**
Mark Brown0be98982008-05-19 12:31:28 +02001434 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001435 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001436 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001437 *
Mark Brown0be98982008-05-19 12:31:28 +02001438 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001439 *
1440 * Returns 0 for success else error.
1441 */
Mark Brown0be98982008-05-19 12:31:28 +02001442int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1443 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001444{
1445 struct snd_soc_codec *codec = socdev->codec;
1446 struct snd_soc_machine *machine = socdev->machine;
Mark Brown0be98982008-05-19 12:31:28 +02001447 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001448
Mark Brown0be98982008-05-19 12:31:28 +02001449 if (machine->set_bias_level)
1450 ret = machine->set_bias_level(machine, level);
1451 if (ret == 0 && codec->set_bias_level)
1452 ret = codec->set_bias_level(codec, level);
1453
1454 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001455}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001456
1457/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001458 * snd_soc_dapm_enable_pin - enable pin.
1459 * @snd_soc_codec: SoC codec
1460 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001461 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001462 * Enables input/output pin and it's parents or children widgets iff there is
1463 * a valid audio route and active audio stream.
1464 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1465 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001466 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001467int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468{
Liam Girdwooda5302182008-07-07 13:35:17 +01001469 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470}
Liam Girdwooda5302182008-07-07 13:35:17 +01001471EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001472
1473/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001474 * snd_soc_dapm_disable_pin - disable pin.
1475 * @codec: SoC codec
1476 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001477 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001478 * Disables input/output pin and it's parents or children widgets.
1479 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1480 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001481 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001482int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1483{
1484 return snd_soc_dapm_set_pin(codec, pin, 0);
1485}
1486EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1487
1488/**
1489 * snd_soc_dapm_get_pin_status - get audio pin status
1490 * @codec: audio codec
1491 * @pin: audio signal pin endpoint (or start point)
1492 *
1493 * Get audio pin status - connected or disconnected.
1494 *
1495 * Returns 1 for connected otherwise 0.
1496 */
1497int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001498{
1499 struct snd_soc_dapm_widget *w;
1500
1501 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001502 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001503 return w->connected;
1504 }
1505
1506 return 0;
1507}
Liam Girdwooda5302182008-07-07 13:35:17 +01001508EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001509
1510/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001511 * snd_soc_dapm_free - free dapm resources
1512 * @socdev: SoC device
1513 *
1514 * Free all dapm widgets and resources.
1515 */
1516void snd_soc_dapm_free(struct snd_soc_device *socdev)
1517{
1518 struct snd_soc_codec *codec = socdev->codec;
1519
1520 snd_soc_dapm_sys_remove(socdev->dev);
1521 dapm_free_widgets(codec);
1522}
1523EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1524
1525/* Module information */
1526MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1527MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1528MODULE_LICENSE("GPL");