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