ASoC: New enum type: value_enum
[safe/jmp/linux-2.6] / sound / soc / soc-dapm.c
1 /*
2  * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6  *
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  *
12  *  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
21  *    o Delayed powerdown of audio susbsystem to reduce pops between a quick
22  *      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>
40 #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 */
47 #ifdef DEBUG
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
49 #else
50 #define dump_dapm(codec, action)
51 #endif
52
53 /* dapm power sequences - make this per codec in the future */
54 static 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_value_mux, snd_soc_dapm_dac,
57         snd_soc_dapm_mixer, snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp,
58         snd_soc_dapm_spk, snd_soc_dapm_post
59 };
60 static 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_value_mux,
64         snd_soc_dapm_post
65 };
66
67 static int dapm_status = 1;
68 module_param(dapm_status, int, 0);
69 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
70
71 static void pop_wait(u32 pop_time)
72 {
73         if (pop_time)
74                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
75 }
76
77 static void pop_dbg(u32 pop_time, const char *fmt, ...)
78 {
79         va_list args;
80
81         va_start(args, fmt);
82
83         if (pop_time) {
84                 vprintk(fmt, args);
85                 pop_wait(pop_time);
86         }
87
88         va_end(args);
89 }
90
91 /* create a new dapm widget */
92 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
93         const struct snd_soc_dapm_widget *_widget)
94 {
95         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
96 }
97
98 /* set up initial codec paths */
99 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
100         struct snd_soc_dapm_path *p, int i)
101 {
102         switch (w->id) {
103         case snd_soc_dapm_switch:
104         case snd_soc_dapm_mixer: {
105                 int val;
106                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
107                         w->kcontrols[i].private_value;
108                 unsigned int reg = mc->reg;
109                 unsigned int shift = mc->shift;
110                 int max = mc->max;
111                 unsigned int mask = (1 << fls(max)) - 1;
112                 unsigned int invert = mc->invert;
113
114                 val = snd_soc_read(w->codec, reg);
115                 val = (val >> shift) & mask;
116
117                 if ((invert && !val) || (!invert && val))
118                         p->connect = 1;
119                 else
120                         p->connect = 0;
121         }
122         break;
123         case snd_soc_dapm_mux: {
124                 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
125                 int val, item, bitmask;
126
127                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
128                 ;
129                 val = snd_soc_read(w->codec, e->reg);
130                 item = (val >> e->shift_l) & (bitmask - 1);
131
132                 p->connect = 0;
133                 for (i = 0; i < e->max; i++) {
134                         if (!(strcmp(p->name, e->texts[i])) && item == i)
135                                 p->connect = 1;
136                 }
137         }
138         break;
139         case snd_soc_dapm_value_mux: {
140                 struct soc_value_enum *e = (struct soc_value_enum *)
141                         w->kcontrols[i].private_value;
142                 int val, item;
143
144                 val = snd_soc_read(w->codec, e->reg);
145                 val = (val >> e->shift_l) & e->mask;
146                 for (item = 0; item < e->max; item++) {
147                         if (val == e->values[item])
148                                 break;
149                 }
150
151                 p->connect = 0;
152                 for (i = 0; i < e->max; i++) {
153                         if (!(strcmp(p->name, e->texts[i])) && item == i)
154                                 p->connect = 1;
155                 }
156         }
157         break;
158         /* does not effect routing - always connected */
159         case snd_soc_dapm_pga:
160         case snd_soc_dapm_output:
161         case snd_soc_dapm_adc:
162         case snd_soc_dapm_input:
163         case snd_soc_dapm_dac:
164         case snd_soc_dapm_micbias:
165         case snd_soc_dapm_vmid:
166                 p->connect = 1;
167         break;
168         /* does effect routing - dynamically connected */
169         case snd_soc_dapm_hp:
170         case snd_soc_dapm_mic:
171         case snd_soc_dapm_spk:
172         case snd_soc_dapm_line:
173         case snd_soc_dapm_pre:
174         case snd_soc_dapm_post:
175                 p->connect = 0;
176         break;
177         }
178 }
179
180 /* connect mux widget to it's interconnecting audio paths */
181 static int dapm_connect_mux(struct snd_soc_codec *codec,
182         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
183         struct snd_soc_dapm_path *path, const char *control_name,
184         const struct snd_kcontrol_new *kcontrol)
185 {
186         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
187         int i;
188
189         for (i = 0; i < e->max; i++) {
190                 if (!(strcmp(control_name, e->texts[i]))) {
191                         list_add(&path->list, &codec->dapm_paths);
192                         list_add(&path->list_sink, &dest->sources);
193                         list_add(&path->list_source, &src->sinks);
194                         path->name = (char*)e->texts[i];
195                         dapm_set_path_status(dest, path, 0);
196                         return 0;
197                 }
198         }
199
200         return -ENODEV;
201 }
202
203 /* connect value_mux widget to it's interconnecting audio paths */
204 static int dapm_connect_value_mux(struct snd_soc_codec *codec,
205         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
206         struct snd_soc_dapm_path *path, const char *control_name,
207         const struct snd_kcontrol_new *kcontrol)
208 {
209         struct soc_value_enum *e = (struct soc_value_enum *)
210                         kcontrol->private_value;
211         int i;
212
213         for (i = 0; i < e->max; i++) {
214                 if (!(strcmp(control_name, e->texts[i]))) {
215                         list_add(&path->list, &codec->dapm_paths);
216                         list_add(&path->list_sink, &dest->sources);
217                         list_add(&path->list_source, &src->sinks);
218                         path->name = (char *)e->texts[i];
219                         dapm_set_path_status(dest, path, 0);
220                         return 0;
221                 }
222         }
223
224         return -ENODEV;
225 }
226
227 /* connect mixer widget to it's interconnecting audio paths */
228 static int dapm_connect_mixer(struct snd_soc_codec *codec,
229         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
230         struct snd_soc_dapm_path *path, const char *control_name)
231 {
232         int i;
233
234         /* search for mixer kcontrol */
235         for (i = 0; i < dest->num_kcontrols; i++) {
236                 if (!strcmp(control_name, dest->kcontrols[i].name)) {
237                         list_add(&path->list, &codec->dapm_paths);
238                         list_add(&path->list_sink, &dest->sources);
239                         list_add(&path->list_source, &src->sinks);
240                         path->name = dest->kcontrols[i].name;
241                         dapm_set_path_status(dest, path, i);
242                         return 0;
243                 }
244         }
245         return -ENODEV;
246 }
247
248 /* update dapm codec register bits */
249 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
250 {
251         int change, power;
252         unsigned short old, new;
253         struct snd_soc_codec *codec = widget->codec;
254
255         /* check for valid widgets */
256         if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
257                 widget->id == snd_soc_dapm_output ||
258                 widget->id == snd_soc_dapm_hp ||
259                 widget->id == snd_soc_dapm_mic ||
260                 widget->id == snd_soc_dapm_line ||
261                 widget->id == snd_soc_dapm_spk)
262                 return 0;
263
264         power = widget->power;
265         if (widget->invert)
266                 power = (power ? 0:1);
267
268         old = snd_soc_read(codec, widget->reg);
269         new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
270
271         change = old != new;
272         if (change) {
273                 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
274                         widget->name, widget->power ? "on" : "off",
275                         codec->pop_time);
276                 snd_soc_write(codec, widget->reg, new);
277                 pop_wait(codec->pop_time);
278         }
279         pr_debug("reg %x old %x new %x change %d\n", widget->reg,
280                  old, new, change);
281         return change;
282 }
283
284 /* ramps the volume up or down to minimise pops before or after a
285  * DAPM power event */
286 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
287 {
288         const struct snd_kcontrol_new *k = widget->kcontrols;
289
290         if (widget->muted && !power)
291                 return 0;
292         if (!widget->muted && power)
293                 return 0;
294
295         if (widget->num_kcontrols && k) {
296                 struct soc_mixer_control *mc =
297                         (struct soc_mixer_control *)k->private_value;
298                 unsigned int reg = mc->reg;
299                 unsigned int shift = mc->shift;
300                 int max = mc->max;
301                 unsigned int mask = (1 << fls(max)) - 1;
302                 unsigned int invert = mc->invert;
303
304                 if (power) {
305                         int i;
306                         /* power up has happended, increase volume to last level */
307                         if (invert) {
308                                 for (i = max; i > widget->saved_value; i--)
309                                         snd_soc_update_bits(widget->codec, reg, mask, i);
310                         } else {
311                                 for (i = 0; i < widget->saved_value; i++)
312                                         snd_soc_update_bits(widget->codec, reg, mask, i);
313                         }
314                         widget->muted = 0;
315                 } else {
316                         /* power down is about to occur, decrease volume to mute */
317                         int val = snd_soc_read(widget->codec, reg);
318                         int i = widget->saved_value = (val >> shift) & mask;
319                         if (invert) {
320                                 for (; i < mask; i++)
321                                         snd_soc_update_bits(widget->codec, reg, mask, i);
322                         } else {
323                                 for (; i > 0; i--)
324                                         snd_soc_update_bits(widget->codec, reg, mask, i);
325                         }
326                         widget->muted = 1;
327                 }
328         }
329         return 0;
330 }
331
332 /* create new dapm mixer control */
333 static int dapm_new_mixer(struct snd_soc_codec *codec,
334         struct snd_soc_dapm_widget *w)
335 {
336         int i, ret = 0;
337         size_t name_len;
338         struct snd_soc_dapm_path *path;
339
340         /* add kcontrol */
341         for (i = 0; i < w->num_kcontrols; i++) {
342
343                 /* match name */
344                 list_for_each_entry(path, &w->sources, list_sink) {
345
346                         /* mixer/mux paths name must match control name */
347                         if (path->name != (char*)w->kcontrols[i].name)
348                                 continue;
349
350                         /* add dapm control with long name */
351                         name_len = 2 + strlen(w->name)
352                                 + strlen(w->kcontrols[i].name);
353                         path->long_name = kmalloc(name_len, GFP_KERNEL);
354                         if (path->long_name == NULL)
355                                 return -ENOMEM;
356
357                         snprintf(path->long_name, name_len, "%s %s",
358                                  w->name, w->kcontrols[i].name);
359                         path->long_name[name_len - 1] = '\0';
360
361                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
362                                 path->long_name);
363                         ret = snd_ctl_add(codec->card, path->kcontrol);
364                         if (ret < 0) {
365                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
366                                                 path->long_name);
367                                 kfree(path->long_name);
368                                 path->long_name = NULL;
369                                 return ret;
370                         }
371                 }
372         }
373         return ret;
374 }
375
376 /* create new dapm mux control */
377 static int dapm_new_mux(struct snd_soc_codec *codec,
378         struct snd_soc_dapm_widget *w)
379 {
380         struct snd_soc_dapm_path *path = NULL;
381         struct snd_kcontrol *kcontrol;
382         int ret = 0;
383
384         if (!w->num_kcontrols) {
385                 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
386                 return -EINVAL;
387         }
388
389         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
390         ret = snd_ctl_add(codec->card, kcontrol);
391         if (ret < 0)
392                 goto err;
393
394         list_for_each_entry(path, &w->sources, list_sink)
395                 path->kcontrol = kcontrol;
396
397         return ret;
398
399 err:
400         printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
401         return ret;
402 }
403
404 /* create new dapm volume control */
405 static int dapm_new_pga(struct snd_soc_codec *codec,
406         struct snd_soc_dapm_widget *w)
407 {
408         struct snd_kcontrol *kcontrol;
409         int ret = 0;
410
411         if (!w->num_kcontrols)
412                 return -EINVAL;
413
414         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
415         ret = snd_ctl_add(codec->card, kcontrol);
416         if (ret < 0) {
417                 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
418                 return ret;
419         }
420
421         return ret;
422 }
423
424 /* reset 'walked' bit for each dapm path */
425 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
426 {
427         struct snd_soc_dapm_path *p;
428
429         list_for_each_entry(p, &codec->dapm_paths, list)
430                 p->walked = 0;
431 }
432
433 /*
434  * Recursively check for a completed path to an active or physically connected
435  * output widget. Returns number of complete paths.
436  */
437 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
438 {
439         struct snd_soc_dapm_path *path;
440         int con = 0;
441
442         if (widget->id == snd_soc_dapm_adc && widget->active)
443                 return 1;
444
445         if (widget->connected) {
446                 /* connected pin ? */
447                 if (widget->id == snd_soc_dapm_output && !widget->ext)
448                         return 1;
449
450                 /* connected jack or spk ? */
451                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
452                         widget->id == snd_soc_dapm_line)
453                         return 1;
454         }
455
456         list_for_each_entry(path, &widget->sinks, list_source) {
457                 if (path->walked)
458                         continue;
459
460                 if (path->sink && path->connect) {
461                         path->walked = 1;
462                         con += is_connected_output_ep(path->sink);
463                 }
464         }
465
466         return con;
467 }
468
469 /*
470  * Recursively check for a completed path to an active or physically connected
471  * input widget. Returns number of complete paths.
472  */
473 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
474 {
475         struct snd_soc_dapm_path *path;
476         int con = 0;
477
478         /* active stream ? */
479         if (widget->id == snd_soc_dapm_dac && widget->active)
480                 return 1;
481
482         if (widget->connected) {
483                 /* connected pin ? */
484                 if (widget->id == snd_soc_dapm_input && !widget->ext)
485                         return 1;
486
487                 /* connected VMID/Bias for lower pops */
488                 if (widget->id == snd_soc_dapm_vmid)
489                         return 1;
490
491                 /* connected jack ? */
492                 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
493                         return 1;
494         }
495
496         list_for_each_entry(path, &widget->sources, list_sink) {
497                 if (path->walked)
498                         continue;
499
500                 if (path->source && path->connect) {
501                         path->walked = 1;
502                         con += is_connected_input_ep(path->source);
503                 }
504         }
505
506         return con;
507 }
508
509 /*
510  * Handler for generic register modifier widget.
511  */
512 int dapm_reg_event(struct snd_soc_dapm_widget *w,
513                    struct snd_kcontrol *kcontrol, int event)
514 {
515         unsigned int val;
516
517         if (SND_SOC_DAPM_EVENT_ON(event))
518                 val = w->on_val;
519         else
520                 val = w->off_val;
521
522         snd_soc_update_bits(w->codec, -(w->reg + 1),
523                             w->mask << w->shift, val << w->shift);
524
525         return 0;
526 }
527 EXPORT_SYMBOL_GPL(dapm_reg_event);
528
529 /*
530  * Scan each dapm widget for complete audio path.
531  * A complete path is a route that has valid endpoints i.e.:-
532  *
533  *  o DAC to output pin.
534  *  o Input Pin to ADC.
535  *  o Input pin to Output pin (bypass, sidetone)
536  *  o DAC to ADC (loopback).
537  */
538 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
539 {
540         struct snd_soc_dapm_widget *w;
541         int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
542
543         /* do we have a sequenced stream event */
544         if (event == SND_SOC_DAPM_STREAM_START) {
545                 c = ARRAY_SIZE(dapm_up_seq);
546                 seq = dapm_up_seq;
547         } else if (event == SND_SOC_DAPM_STREAM_STOP) {
548                 c = ARRAY_SIZE(dapm_down_seq);
549                 seq = dapm_down_seq;
550         }
551
552         for(i = 0; i < c; i++) {
553                 list_for_each_entry(w, &codec->dapm_widgets, list) {
554
555                         /* is widget in stream order */
556                         if (seq && seq[i] && w->id != seq[i])
557                                 continue;
558
559                         /* vmid - no action */
560                         if (w->id == snd_soc_dapm_vmid)
561                                 continue;
562
563                         /* active ADC */
564                         if (w->id == snd_soc_dapm_adc && w->active) {
565                                 in = is_connected_input_ep(w);
566                                 dapm_clear_walk(w->codec);
567                                 w->power = (in != 0) ? 1 : 0;
568                                 dapm_update_bits(w);
569                                 continue;
570                         }
571
572                         /* active DAC */
573                         if (w->id == snd_soc_dapm_dac && w->active) {
574                                 out = is_connected_output_ep(w);
575                                 dapm_clear_walk(w->codec);
576                                 w->power = (out != 0) ? 1 : 0;
577                                 dapm_update_bits(w);
578                                 continue;
579                         }
580
581                         /* pre and post event widgets */
582                         if (w->id == snd_soc_dapm_pre) {
583                                 if (!w->event)
584                                         continue;
585
586                                 if (event == SND_SOC_DAPM_STREAM_START) {
587                                         ret = w->event(w,
588                                                 NULL, SND_SOC_DAPM_PRE_PMU);
589                                         if (ret < 0)
590                                                 return ret;
591                                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
592                                         ret = w->event(w,
593                                                 NULL, SND_SOC_DAPM_PRE_PMD);
594                                         if (ret < 0)
595                                                 return ret;
596                                 }
597                                 continue;
598                         }
599                         if (w->id == snd_soc_dapm_post) {
600                                 if (!w->event)
601                                         continue;
602
603                                 if (event == SND_SOC_DAPM_STREAM_START) {
604                                         ret = w->event(w,
605                                                 NULL, SND_SOC_DAPM_POST_PMU);
606                                         if (ret < 0)
607                                                 return ret;
608                                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
609                                         ret = w->event(w,
610                                                 NULL, SND_SOC_DAPM_POST_PMD);
611                                         if (ret < 0)
612                                                 return ret;
613                                 }
614                                 continue;
615                         }
616
617                         /* all other widgets */
618                         in = is_connected_input_ep(w);
619                         dapm_clear_walk(w->codec);
620                         out = is_connected_output_ep(w);
621                         dapm_clear_walk(w->codec);
622                         power = (out != 0 && in != 0) ? 1 : 0;
623                         power_change = (w->power == power) ? 0: 1;
624                         w->power = power;
625
626                         if (!power_change)
627                                 continue;
628
629                         /* call any power change event handlers */
630                         if (w->event)
631                                 pr_debug("power %s event for %s flags %x\n",
632                                          w->power ? "on" : "off",
633                                          w->name, w->event_flags);
634
635                         /* power up pre event */
636                         if (power && w->event &&
637                             (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
638                                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
639                                 if (ret < 0)
640                                         return ret;
641                         }
642
643                         /* power down pre event */
644                         if (!power && w->event &&
645                             (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
646                                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
647                                 if (ret < 0)
648                                         return ret;
649                         }
650
651                         /* Lower PGA volume to reduce pops */
652                         if (w->id == snd_soc_dapm_pga && !power)
653                                 dapm_set_pga(w, power);
654
655                         dapm_update_bits(w);
656
657                         /* Raise PGA volume to reduce pops */
658                         if (w->id == snd_soc_dapm_pga && power)
659                                 dapm_set_pga(w, power);
660
661                         /* power up post event */
662                         if (power && w->event &&
663                             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
664                                 ret = w->event(w,
665                                                NULL, SND_SOC_DAPM_POST_PMU);
666                                 if (ret < 0)
667                                         return ret;
668                         }
669
670                         /* power down post event */
671                         if (!power && w->event &&
672                             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
673                                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
674                                 if (ret < 0)
675                                         return ret;
676                         }
677                 }
678         }
679
680         return ret;
681 }
682
683 #ifdef DEBUG
684 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
685 {
686         struct snd_soc_dapm_widget *w;
687         struct snd_soc_dapm_path *p = NULL;
688         int in, out;
689
690         printk("DAPM %s %s\n", codec->name, action);
691
692         list_for_each_entry(w, &codec->dapm_widgets, list) {
693
694                 /* only display widgets that effect routing */
695                 switch (w->id) {
696                 case snd_soc_dapm_pre:
697                 case snd_soc_dapm_post:
698                 case snd_soc_dapm_vmid:
699                         continue;
700                 case snd_soc_dapm_mux:
701                 case snd_soc_dapm_value_mux:
702                 case snd_soc_dapm_output:
703                 case snd_soc_dapm_input:
704                 case snd_soc_dapm_switch:
705                 case snd_soc_dapm_hp:
706                 case snd_soc_dapm_mic:
707                 case snd_soc_dapm_spk:
708                 case snd_soc_dapm_line:
709                 case snd_soc_dapm_micbias:
710                 case snd_soc_dapm_dac:
711                 case snd_soc_dapm_adc:
712                 case snd_soc_dapm_pga:
713                 case snd_soc_dapm_mixer:
714                         if (w->name) {
715                                 in = is_connected_input_ep(w);
716                                 dapm_clear_walk(w->codec);
717                                 out = is_connected_output_ep(w);
718                                 dapm_clear_walk(w->codec);
719                                 printk("%s: %s  in %d out %d\n", w->name,
720                                         w->power ? "On":"Off",in, out);
721
722                                 list_for_each_entry(p, &w->sources, list_sink) {
723                                         if (p->connect)
724                                                 printk(" in  %s %s\n", p->name ? p->name : "static",
725                                                         p->source->name);
726                                 }
727                                 list_for_each_entry(p, &w->sinks, list_source) {
728                                         if (p->connect)
729                                                 printk(" out %s %s\n", p->name ? p->name : "static",
730                                                         p->sink->name);
731                                 }
732                         }
733                 break;
734                 }
735         }
736 }
737 #endif
738
739 /* test and update the power status of a mux widget */
740 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
741                                  struct snd_kcontrol *kcontrol, int mask,
742                                  int mux, int val, struct soc_enum *e)
743 {
744         struct snd_soc_dapm_path *path;
745         int found = 0;
746
747         if (widget->id != snd_soc_dapm_mux)
748                 return -ENODEV;
749
750         if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
751                 return 0;
752
753         /* find dapm widget path assoc with kcontrol */
754         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
755                 if (path->kcontrol != kcontrol)
756                         continue;
757
758                 if (!path->name || !e->texts[mux])
759                         continue;
760
761                 found = 1;
762                 /* we now need to match the string in the enum to the path */
763                 if (!(strcmp(path->name, e->texts[mux])))
764                         path->connect = 1; /* new connection */
765                 else
766                         path->connect = 0; /* old connection must be powered down */
767         }
768
769         if (found) {
770                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
771                 dump_dapm(widget->codec, "mux power update");
772         }
773
774         return 0;
775 }
776
777 /* test and update the power status of a value_mux widget */
778 static int dapm_value_mux_update_power(struct snd_soc_dapm_widget *widget,
779                                  struct snd_kcontrol *kcontrol, int mask,
780                                  int mux, int val, struct soc_value_enum *e)
781 {
782         struct snd_soc_dapm_path *path;
783         int found = 0;
784
785         if (widget->id != snd_soc_dapm_value_mux)
786                 return -ENODEV;
787
788         if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
789                 return 0;
790
791         /* find dapm widget path assoc with kcontrol */
792         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
793                 if (path->kcontrol != kcontrol)
794                         continue;
795
796                 if (!path->name || !e->texts[mux])
797                         continue;
798
799                 found = 1;
800                 /* we now need to match the string in the enum to the path */
801                 if (!(strcmp(path->name, e->texts[mux])))
802                         path->connect = 1; /* new connection */
803                 else
804                         path->connect = 0; /* old connection must be
805                                               powered down */
806         }
807
808         if (found) {
809                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
810                 dump_dapm(widget->codec, "mux power update");
811         }
812
813         return 0;
814 }
815
816 /* test and update the power status of a mixer or switch widget */
817 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
818                                    struct snd_kcontrol *kcontrol, int reg,
819                                    int val_mask, int val, int invert)
820 {
821         struct snd_soc_dapm_path *path;
822         int found = 0;
823
824         if (widget->id != snd_soc_dapm_mixer &&
825             widget->id != snd_soc_dapm_switch)
826                 return -ENODEV;
827
828         if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
829                 return 0;
830
831         /* find dapm widget path assoc with kcontrol */
832         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
833                 if (path->kcontrol != kcontrol)
834                         continue;
835
836                 /* found, now check type */
837                 found = 1;
838                 if (val)
839                         /* new connection */
840                         path->connect = invert ? 0:1;
841                 else
842                         /* old connection must be powered down */
843                         path->connect = invert ? 1:0;
844                 break;
845         }
846
847         if (found) {
848                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
849                 dump_dapm(widget->codec, "mixer power update");
850         }
851
852         return 0;
853 }
854
855 /* show dapm widget status in sys fs */
856 static ssize_t dapm_widget_show(struct device *dev,
857         struct device_attribute *attr, char *buf)
858 {
859         struct snd_soc_device *devdata = dev_get_drvdata(dev);
860         struct snd_soc_codec *codec = devdata->codec;
861         struct snd_soc_dapm_widget *w;
862         int count = 0;
863         char *state = "not set";
864
865         list_for_each_entry(w, &codec->dapm_widgets, list) {
866
867                 /* only display widgets that burnm power */
868                 switch (w->id) {
869                 case snd_soc_dapm_hp:
870                 case snd_soc_dapm_mic:
871                 case snd_soc_dapm_spk:
872                 case snd_soc_dapm_line:
873                 case snd_soc_dapm_micbias:
874                 case snd_soc_dapm_dac:
875                 case snd_soc_dapm_adc:
876                 case snd_soc_dapm_pga:
877                 case snd_soc_dapm_mixer:
878                         if (w->name)
879                                 count += sprintf(buf + count, "%s: %s\n",
880                                         w->name, w->power ? "On":"Off");
881                 break;
882                 default:
883                 break;
884                 }
885         }
886
887         switch (codec->bias_level) {
888         case SND_SOC_BIAS_ON:
889                 state = "On";
890                 break;
891         case SND_SOC_BIAS_PREPARE:
892                 state = "Prepare";
893                 break;
894         case SND_SOC_BIAS_STANDBY:
895                 state = "Standby";
896                 break;
897         case SND_SOC_BIAS_OFF:
898                 state = "Off";
899                 break;
900         }
901         count += sprintf(buf + count, "PM State: %s\n", state);
902
903         return count;
904 }
905
906 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
907
908 int snd_soc_dapm_sys_add(struct device *dev)
909 {
910         if (!dapm_status)
911                 return 0;
912         return device_create_file(dev, &dev_attr_dapm_widget);
913 }
914
915 static void snd_soc_dapm_sys_remove(struct device *dev)
916 {
917         if (dapm_status) {
918                 device_remove_file(dev, &dev_attr_dapm_widget);
919         }
920 }
921
922 /* free all dapm widgets and resources */
923 static void dapm_free_widgets(struct snd_soc_codec *codec)
924 {
925         struct snd_soc_dapm_widget *w, *next_w;
926         struct snd_soc_dapm_path *p, *next_p;
927
928         list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
929                 list_del(&w->list);
930                 kfree(w);
931         }
932
933         list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
934                 list_del(&p->list);
935                 kfree(p->long_name);
936                 kfree(p);
937         }
938 }
939
940 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
941         char *pin, int status)
942 {
943         struct snd_soc_dapm_widget *w;
944
945         list_for_each_entry(w, &codec->dapm_widgets, list) {
946                 if (!strcmp(w->name, pin)) {
947                         pr_debug("dapm: %s: pin %s\n", codec->name, pin);
948                         w->connected = status;
949                         return 0;
950                 }
951         }
952
953         pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
954         return -EINVAL;
955 }
956
957 /**
958  * snd_soc_dapm_sync - scan and power dapm paths
959  * @codec: audio codec
960  *
961  * Walks all dapm audio paths and powers widgets according to their
962  * stream or path usage.
963  *
964  * Returns 0 for success.
965  */
966 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
967 {
968         int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
969         dump_dapm(codec, "sync");
970         return ret;
971 }
972 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
973
974 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
975         const char *sink, const char *control, const char *source)
976 {
977         struct snd_soc_dapm_path *path;
978         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
979         int ret = 0;
980
981         /* find src and dest widgets */
982         list_for_each_entry(w, &codec->dapm_widgets, list) {
983
984                 if (!wsink && !(strcmp(w->name, sink))) {
985                         wsink = w;
986                         continue;
987                 }
988                 if (!wsource && !(strcmp(w->name, source))) {
989                         wsource = w;
990                 }
991         }
992
993         if (wsource == NULL || wsink == NULL)
994                 return -ENODEV;
995
996         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
997         if (!path)
998                 return -ENOMEM;
999
1000         path->source = wsource;
1001         path->sink = wsink;
1002         INIT_LIST_HEAD(&path->list);
1003         INIT_LIST_HEAD(&path->list_source);
1004         INIT_LIST_HEAD(&path->list_sink);
1005
1006         /* check for external widgets */
1007         if (wsink->id == snd_soc_dapm_input) {
1008                 if (wsource->id == snd_soc_dapm_micbias ||
1009                         wsource->id == snd_soc_dapm_mic ||
1010                         wsink->id == snd_soc_dapm_line ||
1011                         wsink->id == snd_soc_dapm_output)
1012                         wsink->ext = 1;
1013         }
1014         if (wsource->id == snd_soc_dapm_output) {
1015                 if (wsink->id == snd_soc_dapm_spk ||
1016                         wsink->id == snd_soc_dapm_hp ||
1017                         wsink->id == snd_soc_dapm_line ||
1018                         wsink->id == snd_soc_dapm_input)
1019                         wsource->ext = 1;
1020         }
1021
1022         /* connect static paths */
1023         if (control == NULL) {
1024                 list_add(&path->list, &codec->dapm_paths);
1025                 list_add(&path->list_sink, &wsink->sources);
1026                 list_add(&path->list_source, &wsource->sinks);
1027                 path->connect = 1;
1028                 return 0;
1029         }
1030
1031         /* connect dynamic paths */
1032         switch(wsink->id) {
1033         case snd_soc_dapm_adc:
1034         case snd_soc_dapm_dac:
1035         case snd_soc_dapm_pga:
1036         case snd_soc_dapm_input:
1037         case snd_soc_dapm_output:
1038         case snd_soc_dapm_micbias:
1039         case snd_soc_dapm_vmid:
1040         case snd_soc_dapm_pre:
1041         case snd_soc_dapm_post:
1042                 list_add(&path->list, &codec->dapm_paths);
1043                 list_add(&path->list_sink, &wsink->sources);
1044                 list_add(&path->list_source, &wsource->sinks);
1045                 path->connect = 1;
1046                 return 0;
1047         case snd_soc_dapm_mux:
1048                 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1049                         &wsink->kcontrols[0]);
1050                 if (ret != 0)
1051                         goto err;
1052                 break;
1053         case snd_soc_dapm_value_mux:
1054                 ret = dapm_connect_value_mux(codec, wsource, wsink, path,
1055                         control, &wsink->kcontrols[0]);
1056                 if (ret != 0)
1057                         goto err;
1058                 break;
1059         case snd_soc_dapm_switch:
1060         case snd_soc_dapm_mixer:
1061                 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1062                 if (ret != 0)
1063                         goto err;
1064                 break;
1065         case snd_soc_dapm_hp:
1066         case snd_soc_dapm_mic:
1067         case snd_soc_dapm_line:
1068         case snd_soc_dapm_spk:
1069                 list_add(&path->list, &codec->dapm_paths);
1070                 list_add(&path->list_sink, &wsink->sources);
1071                 list_add(&path->list_source, &wsource->sinks);
1072                 path->connect = 0;
1073                 return 0;
1074         }
1075         return 0;
1076
1077 err:
1078         printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1079                 control, sink);
1080         kfree(path);
1081         return ret;
1082 }
1083
1084 /**
1085  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1086  * @codec: codec
1087  * @route: audio routes
1088  * @num: number of routes
1089  *
1090  * Connects 2 dapm widgets together via a named audio path. The sink is
1091  * the widget receiving the audio signal, whilst the source is the sender
1092  * of the audio signal.
1093  *
1094  * Returns 0 for success else error. On error all resources can be freed
1095  * with a call to snd_soc_card_free().
1096  */
1097 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1098                             const struct snd_soc_dapm_route *route, int num)
1099 {
1100         int i, ret;
1101
1102         for (i = 0; i < num; i++) {
1103                 ret = snd_soc_dapm_add_route(codec, route->sink,
1104                                              route->control, route->source);
1105                 if (ret < 0) {
1106                         printk(KERN_ERR "Failed to add route %s->%s\n",
1107                                route->source,
1108                                route->sink);
1109                         return ret;
1110                 }
1111                 route++;
1112         }
1113
1114         return 0;
1115 }
1116 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1117
1118 /**
1119  * snd_soc_dapm_new_widgets - add new dapm widgets
1120  * @codec: audio codec
1121  *
1122  * Checks the codec for any new dapm widgets and creates them if found.
1123  *
1124  * Returns 0 for success.
1125  */
1126 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1127 {
1128         struct snd_soc_dapm_widget *w;
1129
1130         list_for_each_entry(w, &codec->dapm_widgets, list)
1131         {
1132                 if (w->new)
1133                         continue;
1134
1135                 switch(w->id) {
1136                 case snd_soc_dapm_switch:
1137                 case snd_soc_dapm_mixer:
1138                         dapm_new_mixer(codec, w);
1139                         break;
1140                 case snd_soc_dapm_mux:
1141                 case snd_soc_dapm_value_mux:
1142                         dapm_new_mux(codec, w);
1143                         break;
1144                 case snd_soc_dapm_adc:
1145                 case snd_soc_dapm_dac:
1146                 case snd_soc_dapm_pga:
1147                         dapm_new_pga(codec, w);
1148                         break;
1149                 case snd_soc_dapm_input:
1150                 case snd_soc_dapm_output:
1151                 case snd_soc_dapm_micbias:
1152                 case snd_soc_dapm_spk:
1153                 case snd_soc_dapm_hp:
1154                 case snd_soc_dapm_mic:
1155                 case snd_soc_dapm_line:
1156                 case snd_soc_dapm_vmid:
1157                 case snd_soc_dapm_pre:
1158                 case snd_soc_dapm_post:
1159                         break;
1160                 }
1161                 w->new = 1;
1162         }
1163
1164         dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1165         return 0;
1166 }
1167 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1168
1169 /**
1170  * snd_soc_dapm_get_volsw - dapm mixer get callback
1171  * @kcontrol: mixer control
1172  * @ucontrol: control element information
1173  *
1174  * Callback to get the value of a dapm mixer control.
1175  *
1176  * Returns 0 for success.
1177  */
1178 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1179         struct snd_ctl_elem_value *ucontrol)
1180 {
1181         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1182         struct soc_mixer_control *mc =
1183                 (struct soc_mixer_control *)kcontrol->private_value;
1184         unsigned int reg = mc->reg;
1185         unsigned int shift = mc->shift;
1186         unsigned int rshift = mc->rshift;
1187         int max = mc->max;
1188         unsigned int invert = mc->invert;
1189         unsigned int mask = (1 << fls(max)) - 1;
1190
1191         /* return the saved value if we are powered down */
1192         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1193                 ucontrol->value.integer.value[0] = widget->saved_value;
1194                 return 0;
1195         }
1196
1197         ucontrol->value.integer.value[0] =
1198                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1199         if (shift != rshift)
1200                 ucontrol->value.integer.value[1] =
1201                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1202         if (invert) {
1203                 ucontrol->value.integer.value[0] =
1204                         max - ucontrol->value.integer.value[0];
1205                 if (shift != rshift)
1206                         ucontrol->value.integer.value[1] =
1207                                 max - ucontrol->value.integer.value[1];
1208         }
1209
1210         return 0;
1211 }
1212 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1213
1214 /**
1215  * snd_soc_dapm_put_volsw - dapm mixer set callback
1216  * @kcontrol: mixer control
1217  * @ucontrol: control element information
1218  *
1219  * Callback to set the value of a dapm mixer control.
1220  *
1221  * Returns 0 for success.
1222  */
1223 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1224         struct snd_ctl_elem_value *ucontrol)
1225 {
1226         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1227         struct soc_mixer_control *mc =
1228                 (struct soc_mixer_control *)kcontrol->private_value;
1229         unsigned int reg = mc->reg;
1230         unsigned int shift = mc->shift;
1231         unsigned int rshift = mc->rshift;
1232         int max = mc->max;
1233         unsigned int mask = (1 << fls(max)) - 1;
1234         unsigned int invert = mc->invert;
1235         unsigned short val, val2, val_mask;
1236         int ret;
1237
1238         val = (ucontrol->value.integer.value[0] & mask);
1239
1240         if (invert)
1241                 val = max - val;
1242         val_mask = mask << shift;
1243         val = val << shift;
1244         if (shift != rshift) {
1245                 val2 = (ucontrol->value.integer.value[1] & mask);
1246                 if (invert)
1247                         val2 = max - val2;
1248                 val_mask |= mask << rshift;
1249                 val |= val2 << rshift;
1250         }
1251
1252         mutex_lock(&widget->codec->mutex);
1253         widget->value = val;
1254
1255         /* save volume value if the widget is powered down */
1256         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1257                 widget->saved_value = val;
1258                 mutex_unlock(&widget->codec->mutex);
1259                 return 1;
1260         }
1261
1262         dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1263         if (widget->event) {
1264                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1265                         ret = widget->event(widget, kcontrol,
1266                                                 SND_SOC_DAPM_PRE_REG);
1267                         if (ret < 0) {
1268                                 ret = 1;
1269                                 goto out;
1270                         }
1271                 }
1272                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1273                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1274                         ret = widget->event(widget, kcontrol,
1275                                                 SND_SOC_DAPM_POST_REG);
1276         } else
1277                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1278
1279 out:
1280         mutex_unlock(&widget->codec->mutex);
1281         return ret;
1282 }
1283 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1284
1285 /**
1286  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1287  * @kcontrol: mixer control
1288  * @ucontrol: control element information
1289  *
1290  * Callback to get the value of a dapm enumerated double mixer control.
1291  *
1292  * Returns 0 for success.
1293  */
1294 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1295         struct snd_ctl_elem_value *ucontrol)
1296 {
1297         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1298         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1299         unsigned short val, bitmask;
1300
1301         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1302                 ;
1303         val = snd_soc_read(widget->codec, e->reg);
1304         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1305         if (e->shift_l != e->shift_r)
1306                 ucontrol->value.enumerated.item[1] =
1307                         (val >> e->shift_r) & (bitmask - 1);
1308
1309         return 0;
1310 }
1311 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1312
1313 /**
1314  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1315  * @kcontrol: mixer control
1316  * @ucontrol: control element information
1317  *
1318  * Callback to set the value of a dapm enumerated double mixer control.
1319  *
1320  * Returns 0 for success.
1321  */
1322 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1323         struct snd_ctl_elem_value *ucontrol)
1324 {
1325         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1326         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1327         unsigned short val, mux;
1328         unsigned short mask, bitmask;
1329         int ret = 0;
1330
1331         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1332                 ;
1333         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1334                 return -EINVAL;
1335         mux = ucontrol->value.enumerated.item[0];
1336         val = mux << e->shift_l;
1337         mask = (bitmask - 1) << e->shift_l;
1338         if (e->shift_l != e->shift_r) {
1339                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1340                         return -EINVAL;
1341                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1342                 mask |= (bitmask - 1) << e->shift_r;
1343         }
1344
1345         mutex_lock(&widget->codec->mutex);
1346         widget->value = val;
1347         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1348         if (widget->event) {
1349                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1350                         ret = widget->event(widget,
1351                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1352                         if (ret < 0)
1353                                 goto out;
1354                 }
1355                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1356                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1357                         ret = widget->event(widget,
1358                                 kcontrol, SND_SOC_DAPM_POST_REG);
1359         } else
1360                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1361
1362 out:
1363         mutex_unlock(&widget->codec->mutex);
1364         return ret;
1365 }
1366 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1367
1368 /**
1369  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1370  *                                      callback
1371  * @kcontrol: mixer control
1372  * @ucontrol: control element information
1373  *
1374  * Callback to get the value of a dapm semi enumerated double mixer control.
1375  *
1376  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1377  * used for handling bitfield coded enumeration for example.
1378  *
1379  * Returns 0 for success.
1380  */
1381 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1382         struct snd_ctl_elem_value *ucontrol)
1383 {
1384         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1385         struct soc_value_enum *e = (struct soc_value_enum *)
1386                         kcontrol->private_value;
1387         unsigned short reg_val, val, mux;
1388
1389         reg_val = snd_soc_read(widget->codec, e->reg);
1390         val = (reg_val >> e->shift_l) & e->mask;
1391         for (mux = 0; mux < e->max; mux++) {
1392                 if (val == e->values[mux])
1393                         break;
1394         }
1395         ucontrol->value.enumerated.item[0] = mux;
1396         if (e->shift_l != e->shift_r) {
1397                 val = (reg_val >> e->shift_r) & e->mask;
1398                 for (mux = 0; mux < e->max; mux++) {
1399                         if (val == e->values[mux])
1400                                 break;
1401                 }
1402                 ucontrol->value.enumerated.item[1] = mux;
1403         }
1404
1405         return 0;
1406 }
1407 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1408
1409 /**
1410  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1411  *                                      callback
1412  * @kcontrol: mixer control
1413  * @ucontrol: control element information
1414  *
1415  * Callback to set the value of a dapm semi enumerated double mixer control.
1416  *
1417  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1418  * used for handling bitfield coded enumeration for example.
1419  *
1420  * Returns 0 for success.
1421  */
1422 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1423         struct snd_ctl_elem_value *ucontrol)
1424 {
1425         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1426         struct soc_value_enum *e = (struct soc_value_enum *)
1427                         kcontrol->private_value;
1428         unsigned short val, mux;
1429         unsigned short mask;
1430         int ret = 0;
1431
1432         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1433                 return -EINVAL;
1434         mux = ucontrol->value.enumerated.item[0];
1435         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1436         mask = e->mask << e->shift_l;
1437         if (e->shift_l != e->shift_r) {
1438                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1439                         return -EINVAL;
1440                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1441                 mask |= e->mask << e->shift_r;
1442         }
1443
1444         mutex_lock(&widget->codec->mutex);
1445         widget->value = val;
1446         dapm_value_mux_update_power(widget, kcontrol, mask, mux, val, e);
1447         if (widget->event) {
1448                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1449                         ret = widget->event(widget,
1450                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1451                         if (ret < 0)
1452                                 goto out;
1453                 }
1454                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1455                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1456                         ret = widget->event(widget,
1457                                 kcontrol, SND_SOC_DAPM_POST_REG);
1458         } else
1459                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1460
1461 out:
1462         mutex_unlock(&widget->codec->mutex);
1463         return ret;
1464 }
1465 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1466
1467 /**
1468  * snd_soc_dapm_new_control - create new dapm control
1469  * @codec: audio codec
1470  * @widget: widget template
1471  *
1472  * Creates a new dapm control based upon the template.
1473  *
1474  * Returns 0 for success else error.
1475  */
1476 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1477         const struct snd_soc_dapm_widget *widget)
1478 {
1479         struct snd_soc_dapm_widget *w;
1480
1481         if ((w = dapm_cnew_widget(widget)) == NULL)
1482                 return -ENOMEM;
1483
1484         w->codec = codec;
1485         INIT_LIST_HEAD(&w->sources);
1486         INIT_LIST_HEAD(&w->sinks);
1487         INIT_LIST_HEAD(&w->list);
1488         list_add(&w->list, &codec->dapm_widgets);
1489
1490         /* machine layer set ups unconnected pins and insertions */
1491         w->connected = 1;
1492         return 0;
1493 }
1494 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1495
1496 /**
1497  * snd_soc_dapm_new_controls - create new dapm controls
1498  * @codec: audio codec
1499  * @widget: widget array
1500  * @num: number of widgets
1501  *
1502  * Creates new DAPM controls based upon the templates.
1503  *
1504  * Returns 0 for success else error.
1505  */
1506 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1507         const struct snd_soc_dapm_widget *widget,
1508         int num)
1509 {
1510         int i, ret;
1511
1512         for (i = 0; i < num; i++) {
1513                 ret = snd_soc_dapm_new_control(codec, widget);
1514                 if (ret < 0) {
1515                         printk(KERN_ERR
1516                                "ASoC: Failed to create DAPM control %s: %d\n",
1517                                widget->name, ret);
1518                         return ret;
1519                 }
1520                 widget++;
1521         }
1522         return 0;
1523 }
1524 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1525
1526
1527 /**
1528  * snd_soc_dapm_stream_event - send a stream event to the dapm core
1529  * @codec: audio codec
1530  * @stream: stream name
1531  * @event: stream event
1532  *
1533  * Sends a stream event to the dapm core. The core then makes any
1534  * necessary widget power changes.
1535  *
1536  * Returns 0 for success else error.
1537  */
1538 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1539         char *stream, int event)
1540 {
1541         struct snd_soc_dapm_widget *w;
1542
1543         if (stream == NULL)
1544                 return 0;
1545
1546         mutex_lock(&codec->mutex);
1547         list_for_each_entry(w, &codec->dapm_widgets, list)
1548         {
1549                 if (!w->sname)
1550                         continue;
1551                 pr_debug("widget %s\n %s stream %s event %d\n",
1552                          w->name, w->sname, stream, event);
1553                 if (strstr(w->sname, stream)) {
1554                         switch(event) {
1555                         case SND_SOC_DAPM_STREAM_START:
1556                                 w->active = 1;
1557                                 break;
1558                         case SND_SOC_DAPM_STREAM_STOP:
1559                                 w->active = 0;
1560                                 break;
1561                         case SND_SOC_DAPM_STREAM_SUSPEND:
1562                                 if (w->active)
1563                                         w->suspend = 1;
1564                                 w->active = 0;
1565                                 break;
1566                         case SND_SOC_DAPM_STREAM_RESUME:
1567                                 if (w->suspend) {
1568                                         w->active = 1;
1569                                         w->suspend = 0;
1570                                 }
1571                                 break;
1572                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1573                                 break;
1574                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1575                                 break;
1576                         }
1577                 }
1578         }
1579         mutex_unlock(&codec->mutex);
1580
1581         dapm_power_widgets(codec, event);
1582         dump_dapm(codec, __func__);
1583         return 0;
1584 }
1585 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1586
1587 /**
1588  * snd_soc_dapm_set_bias_level - set the bias level for the system
1589  * @socdev: audio device
1590  * @level: level to configure
1591  *
1592  * Configure the bias (power) levels for the SoC audio device.
1593  *
1594  * Returns 0 for success else error.
1595  */
1596 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1597                                 enum snd_soc_bias_level level)
1598 {
1599         struct snd_soc_codec *codec = socdev->codec;
1600         struct snd_soc_card *card = socdev->card;
1601         int ret = 0;
1602
1603         if (card->set_bias_level)
1604                 ret = card->set_bias_level(card, level);
1605         if (ret == 0 && codec->set_bias_level)
1606                 ret = codec->set_bias_level(codec, level);
1607
1608         return ret;
1609 }
1610
1611 /**
1612  * snd_soc_dapm_enable_pin - enable pin.
1613  * @codec: SoC codec
1614  * @pin: pin name
1615  *
1616  * Enables input/output pin and it's parents or children widgets iff there is
1617  * a valid audio route and active audio stream.
1618  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1619  * do any widget power switching.
1620  */
1621 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
1622 {
1623         return snd_soc_dapm_set_pin(codec, pin, 1);
1624 }
1625 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1626
1627 /**
1628  * snd_soc_dapm_disable_pin - disable pin.
1629  * @codec: SoC codec
1630  * @pin: pin name
1631  *
1632  * Disables input/output pin and it's parents or children widgets.
1633  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1634  * do any widget power switching.
1635  */
1636 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1637 {
1638         return snd_soc_dapm_set_pin(codec, pin, 0);
1639 }
1640 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1641
1642 /**
1643  * snd_soc_dapm_nc_pin - permanently disable pin.
1644  * @codec: SoC codec
1645  * @pin: pin name
1646  *
1647  * Marks the specified pin as being not connected, disabling it along
1648  * any parent or child widgets.  At present this is identical to
1649  * snd_soc_dapm_disable_pin() but in future it will be extended to do
1650  * additional things such as disabling controls which only affect
1651  * paths through the pin.
1652  *
1653  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1654  * do any widget power switching.
1655  */
1656 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
1657 {
1658         return snd_soc_dapm_set_pin(codec, pin, 0);
1659 }
1660 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1661
1662 /**
1663  * snd_soc_dapm_get_pin_status - get audio pin status
1664  * @codec: audio codec
1665  * @pin: audio signal pin endpoint (or start point)
1666  *
1667  * Get audio pin status - connected or disconnected.
1668  *
1669  * Returns 1 for connected otherwise 0.
1670  */
1671 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
1672 {
1673         struct snd_soc_dapm_widget *w;
1674
1675         list_for_each_entry(w, &codec->dapm_widgets, list) {
1676                 if (!strcmp(w->name, pin))
1677                         return w->connected;
1678         }
1679
1680         return 0;
1681 }
1682 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1683
1684 /**
1685  * snd_soc_dapm_free - free dapm resources
1686  * @socdev: SoC device
1687  *
1688  * Free all dapm widgets and resources.
1689  */
1690 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1691 {
1692         struct snd_soc_codec *codec = socdev->codec;
1693
1694         snd_soc_dapm_sys_remove(socdev->dev);
1695         dapm_free_widgets(codec);
1696 }
1697 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1698
1699 /* Module information */
1700 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1701 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1702 MODULE_LICENSE("GPL");