a6d73379ab32fc78da80bf20f99a227f74388056
[sfrench/cifs-2.6.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_named_ctl)
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                         default:
345                                 snprintf(path->long_name, name_len, "%s %s",
346                                          w->name, w->kcontrols[i].name);
347                                 break;
348                         case snd_soc_dapm_mixer_named_ctl:
349                                 snprintf(path->long_name, name_len, "%s",
350                                          w->kcontrols[i].name);
351                                 break;
352                         }
353
354                         path->long_name[name_len - 1] = '\0';
355
356                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
357                                 path->long_name);
358                         ret = snd_ctl_add(codec->card, path->kcontrol);
359                         if (ret < 0) {
360                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
361                                        path->long_name,
362                                        ret);
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 /* Standard power change method, used to apply power changes to most
526  * widgets.
527  */
528 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
529 {
530         int ret;
531
532         /* call any power change event handlers */
533         if (w->event)
534                 pr_debug("power %s event for %s flags %x\n",
535                          w->power ? "on" : "off",
536                          w->name, w->event_flags);
537
538         /* power up pre event */
539         if (w->power && w->event &&
540             (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
541                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
542                 if (ret < 0)
543                         return ret;
544         }
545
546         /* power down pre event */
547         if (!w->power && w->event &&
548             (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
549                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
550                 if (ret < 0)
551                         return ret;
552         }
553
554         /* Lower PGA volume to reduce pops */
555         if (w->id == snd_soc_dapm_pga && !w->power)
556                 dapm_set_pga(w, w->power);
557
558         dapm_update_bits(w);
559
560         /* Raise PGA volume to reduce pops */
561         if (w->id == snd_soc_dapm_pga && w->power)
562                 dapm_set_pga(w, w->power);
563
564         /* power up post event */
565         if (w->power && w->event &&
566             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
567                 ret = w->event(w,
568                                NULL, SND_SOC_DAPM_POST_PMU);
569                 if (ret < 0)
570                         return ret;
571         }
572
573         /* power down post event */
574         if (!w->power && w->event &&
575             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
576                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
577                 if (ret < 0)
578                         return ret;
579         }
580
581         return 0;
582 }
583
584 /*
585  * Scan a single DAPM widget for a complete audio path and update the
586  * power status appropriately.
587  */
588 static int dapm_power_widget(struct snd_soc_codec *codec, int event,
589                              struct snd_soc_dapm_widget *w)
590 {
591         int in, out, power_change, power, ret;
592
593         /* vmid - no action */
594         if (w->id == snd_soc_dapm_vmid)
595                 return 0;
596
597         /* active ADC */
598         if (w->id == snd_soc_dapm_adc && w->active) {
599                 in = is_connected_input_ep(w);
600                 dapm_clear_walk(w->codec);
601                 power = (in != 0) ? 1 : 0;
602                 if (power == w->power)
603                         return 0;
604                 w->power = power;
605                 return dapm_generic_apply_power(w);
606         }
607
608         /* active DAC */
609         if (w->id == snd_soc_dapm_dac && w->active) {
610                 out = is_connected_output_ep(w);
611                 dapm_clear_walk(w->codec);
612                 power = (out != 0) ? 1 : 0;
613                 if (power == w->power)
614                         return 0;
615                 w->power = power;
616                 return dapm_generic_apply_power(w);
617         }
618
619         /* pre and post event widgets */
620         if (w->id == snd_soc_dapm_pre) {
621                 if (!w->event)
622                         return 0;
623
624                 if (event == SND_SOC_DAPM_STREAM_START) {
625                         ret = w->event(w,
626                                        NULL, SND_SOC_DAPM_PRE_PMU);
627                         if (ret < 0)
628                                 return ret;
629                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
630                         ret = w->event(w,
631                                        NULL, SND_SOC_DAPM_PRE_PMD);
632                         if (ret < 0)
633                                 return ret;
634                 }
635                 return 0;
636         }
637         if (w->id == snd_soc_dapm_post) {
638                 if (!w->event)
639                         return 0;
640
641                 if (event == SND_SOC_DAPM_STREAM_START) {
642                         ret = w->event(w,
643                                        NULL, SND_SOC_DAPM_POST_PMU);
644                         if (ret < 0)
645                                 return ret;
646                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
647                         ret = w->event(w,
648                                        NULL, SND_SOC_DAPM_POST_PMD);
649                         if (ret < 0)
650                                 return ret;
651                 }
652                 return 0;
653         }
654
655         /* all other widgets */
656         in = is_connected_input_ep(w);
657         dapm_clear_walk(w->codec);
658         out = is_connected_output_ep(w);
659         dapm_clear_walk(w->codec);
660         power = (out != 0 && in != 0) ? 1 : 0;
661         power_change = (w->power == power) ? 0 : 1;
662         w->power = power;
663
664         if (!power_change)
665                 return 0;
666
667         return dapm_generic_apply_power(w);
668 }
669
670 /*
671  * Scan each dapm widget for complete audio path.
672  * A complete path is a route that has valid endpoints i.e.:-
673  *
674  *  o DAC to output pin.
675  *  o Input Pin to ADC.
676  *  o Input pin to Output pin (bypass, sidetone)
677  *  o DAC to ADC (loopback).
678  */
679 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
680 {
681         struct snd_soc_dapm_widget *w;
682         int i, c = 1, *seq = NULL, ret = 0;
683
684         /* do we have a sequenced stream event */
685         if (event == SND_SOC_DAPM_STREAM_START) {
686                 c = ARRAY_SIZE(dapm_up_seq);
687                 seq = dapm_up_seq;
688         } else if (event == SND_SOC_DAPM_STREAM_STOP) {
689                 c = ARRAY_SIZE(dapm_down_seq);
690                 seq = dapm_down_seq;
691         }
692
693         for (i = 0; i < c; i++) {
694                 list_for_each_entry(w, &codec->dapm_widgets, list) {
695
696                         /* is widget in stream order */
697                         if (seq && seq[i] && w->id != seq[i])
698                                 continue;
699
700                         ret = dapm_power_widget(codec, event, w);
701                         if (ret != 0)
702                                 return ret;
703                 }
704         }
705
706         return 0;
707 }
708
709 #ifdef DEBUG
710 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
711 {
712         struct snd_soc_dapm_widget *w;
713         struct snd_soc_dapm_path *p = NULL;
714         int in, out;
715
716         printk("DAPM %s %s\n", codec->name, action);
717
718         list_for_each_entry(w, &codec->dapm_widgets, list) {
719
720                 /* only display widgets that effect routing */
721                 switch (w->id) {
722                 case snd_soc_dapm_pre:
723                 case snd_soc_dapm_post:
724                 case snd_soc_dapm_vmid:
725                         continue;
726                 case snd_soc_dapm_mux:
727                 case snd_soc_dapm_value_mux:
728                 case snd_soc_dapm_output:
729                 case snd_soc_dapm_input:
730                 case snd_soc_dapm_switch:
731                 case snd_soc_dapm_hp:
732                 case snd_soc_dapm_mic:
733                 case snd_soc_dapm_spk:
734                 case snd_soc_dapm_line:
735                 case snd_soc_dapm_micbias:
736                 case snd_soc_dapm_dac:
737                 case snd_soc_dapm_adc:
738                 case snd_soc_dapm_pga:
739                 case snd_soc_dapm_mixer:
740                 case snd_soc_dapm_mixer_named_ctl:
741                         if (w->name) {
742                                 in = is_connected_input_ep(w);
743                                 dapm_clear_walk(w->codec);
744                                 out = is_connected_output_ep(w);
745                                 dapm_clear_walk(w->codec);
746                                 printk("%s: %s  in %d out %d\n", w->name,
747                                         w->power ? "On":"Off",in, out);
748
749                                 list_for_each_entry(p, &w->sources, list_sink) {
750                                         if (p->connect)
751                                                 printk(" in  %s %s\n", p->name ? p->name : "static",
752                                                         p->source->name);
753                                 }
754                                 list_for_each_entry(p, &w->sinks, list_source) {
755                                         if (p->connect)
756                                                 printk(" out %s %s\n", p->name ? p->name : "static",
757                                                         p->sink->name);
758                                 }
759                         }
760                 break;
761                 }
762         }
763 }
764 #endif
765
766 /* test and update the power status of a mux widget */
767 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
768                                  struct snd_kcontrol *kcontrol, int mask,
769                                  int mux, int val, struct soc_enum *e)
770 {
771         struct snd_soc_dapm_path *path;
772         int found = 0;
773
774         if (widget->id != snd_soc_dapm_mux &&
775             widget->id != snd_soc_dapm_value_mux)
776                 return -ENODEV;
777
778         if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
779                 return 0;
780
781         /* find dapm widget path assoc with kcontrol */
782         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
783                 if (path->kcontrol != kcontrol)
784                         continue;
785
786                 if (!path->name || !e->texts[mux])
787                         continue;
788
789                 found = 1;
790                 /* we now need to match the string in the enum to the path */
791                 if (!(strcmp(path->name, e->texts[mux])))
792                         path->connect = 1; /* new connection */
793                 else
794                         path->connect = 0; /* old connection must be powered down */
795         }
796
797         if (found) {
798                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
799                 dump_dapm(widget->codec, "mux power update");
800         }
801
802         return 0;
803 }
804
805 /* test and update the power status of a mixer or switch widget */
806 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
807                                    struct snd_kcontrol *kcontrol, int reg,
808                                    int val_mask, int val, int invert)
809 {
810         struct snd_soc_dapm_path *path;
811         int found = 0;
812
813         if (widget->id != snd_soc_dapm_mixer &&
814             widget->id != snd_soc_dapm_mixer_named_ctl &&
815             widget->id != snd_soc_dapm_switch)
816                 return -ENODEV;
817
818         if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
819                 return 0;
820
821         /* find dapm widget path assoc with kcontrol */
822         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
823                 if (path->kcontrol != kcontrol)
824                         continue;
825
826                 /* found, now check type */
827                 found = 1;
828                 if (val)
829                         /* new connection */
830                         path->connect = invert ? 0:1;
831                 else
832                         /* old connection must be powered down */
833                         path->connect = invert ? 1:0;
834                 break;
835         }
836
837         if (found) {
838                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
839                 dump_dapm(widget->codec, "mixer power update");
840         }
841
842         return 0;
843 }
844
845 /* show dapm widget status in sys fs */
846 static ssize_t dapm_widget_show(struct device *dev,
847         struct device_attribute *attr, char *buf)
848 {
849         struct snd_soc_device *devdata = dev_get_drvdata(dev);
850         struct snd_soc_codec *codec = devdata->card->codec;
851         struct snd_soc_dapm_widget *w;
852         int count = 0;
853         char *state = "not set";
854
855         list_for_each_entry(w, &codec->dapm_widgets, list) {
856
857                 /* only display widgets that burnm power */
858                 switch (w->id) {
859                 case snd_soc_dapm_hp:
860                 case snd_soc_dapm_mic:
861                 case snd_soc_dapm_spk:
862                 case snd_soc_dapm_line:
863                 case snd_soc_dapm_micbias:
864                 case snd_soc_dapm_dac:
865                 case snd_soc_dapm_adc:
866                 case snd_soc_dapm_pga:
867                 case snd_soc_dapm_mixer:
868                 case snd_soc_dapm_mixer_named_ctl:
869                         if (w->name)
870                                 count += sprintf(buf + count, "%s: %s\n",
871                                         w->name, w->power ? "On":"Off");
872                 break;
873                 default:
874                 break;
875                 }
876         }
877
878         switch (codec->bias_level) {
879         case SND_SOC_BIAS_ON:
880                 state = "On";
881                 break;
882         case SND_SOC_BIAS_PREPARE:
883                 state = "Prepare";
884                 break;
885         case SND_SOC_BIAS_STANDBY:
886                 state = "Standby";
887                 break;
888         case SND_SOC_BIAS_OFF:
889                 state = "Off";
890                 break;
891         }
892         count += sprintf(buf + count, "PM State: %s\n", state);
893
894         return count;
895 }
896
897 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
898
899 int snd_soc_dapm_sys_add(struct device *dev)
900 {
901         if (!dapm_status)
902                 return 0;
903         return device_create_file(dev, &dev_attr_dapm_widget);
904 }
905
906 static void snd_soc_dapm_sys_remove(struct device *dev)
907 {
908         if (dapm_status) {
909                 device_remove_file(dev, &dev_attr_dapm_widget);
910         }
911 }
912
913 /* free all dapm widgets and resources */
914 static void dapm_free_widgets(struct snd_soc_codec *codec)
915 {
916         struct snd_soc_dapm_widget *w, *next_w;
917         struct snd_soc_dapm_path *p, *next_p;
918
919         list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
920                 list_del(&w->list);
921                 kfree(w);
922         }
923
924         list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
925                 list_del(&p->list);
926                 kfree(p->long_name);
927                 kfree(p);
928         }
929 }
930
931 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
932                                 const char *pin, int status)
933 {
934         struct snd_soc_dapm_widget *w;
935
936         list_for_each_entry(w, &codec->dapm_widgets, list) {
937                 if (!strcmp(w->name, pin)) {
938                         pr_debug("dapm: %s: pin %s\n", codec->name, pin);
939                         w->connected = status;
940                         return 0;
941                 }
942         }
943
944         pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
945         return -EINVAL;
946 }
947
948 /**
949  * snd_soc_dapm_sync - scan and power dapm paths
950  * @codec: audio codec
951  *
952  * Walks all dapm audio paths and powers widgets according to their
953  * stream or path usage.
954  *
955  * Returns 0 for success.
956  */
957 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
958 {
959         int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
960         dump_dapm(codec, "sync");
961         return ret;
962 }
963 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
964
965 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
966         const char *sink, const char *control, const char *source)
967 {
968         struct snd_soc_dapm_path *path;
969         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
970         int ret = 0;
971
972         /* find src and dest widgets */
973         list_for_each_entry(w, &codec->dapm_widgets, list) {
974
975                 if (!wsink && !(strcmp(w->name, sink))) {
976                         wsink = w;
977                         continue;
978                 }
979                 if (!wsource && !(strcmp(w->name, source))) {
980                         wsource = w;
981                 }
982         }
983
984         if (wsource == NULL || wsink == NULL)
985                 return -ENODEV;
986
987         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
988         if (!path)
989                 return -ENOMEM;
990
991         path->source = wsource;
992         path->sink = wsink;
993         INIT_LIST_HEAD(&path->list);
994         INIT_LIST_HEAD(&path->list_source);
995         INIT_LIST_HEAD(&path->list_sink);
996
997         /* check for external widgets */
998         if (wsink->id == snd_soc_dapm_input) {
999                 if (wsource->id == snd_soc_dapm_micbias ||
1000                         wsource->id == snd_soc_dapm_mic ||
1001                         wsink->id == snd_soc_dapm_line ||
1002                         wsink->id == snd_soc_dapm_output)
1003                         wsink->ext = 1;
1004         }
1005         if (wsource->id == snd_soc_dapm_output) {
1006                 if (wsink->id == snd_soc_dapm_spk ||
1007                         wsink->id == snd_soc_dapm_hp ||
1008                         wsink->id == snd_soc_dapm_line ||
1009                         wsink->id == snd_soc_dapm_input)
1010                         wsource->ext = 1;
1011         }
1012
1013         /* connect static paths */
1014         if (control == NULL) {
1015                 list_add(&path->list, &codec->dapm_paths);
1016                 list_add(&path->list_sink, &wsink->sources);
1017                 list_add(&path->list_source, &wsource->sinks);
1018                 path->connect = 1;
1019                 return 0;
1020         }
1021
1022         /* connect dynamic paths */
1023         switch(wsink->id) {
1024         case snd_soc_dapm_adc:
1025         case snd_soc_dapm_dac:
1026         case snd_soc_dapm_pga:
1027         case snd_soc_dapm_input:
1028         case snd_soc_dapm_output:
1029         case snd_soc_dapm_micbias:
1030         case snd_soc_dapm_vmid:
1031         case snd_soc_dapm_pre:
1032         case snd_soc_dapm_post:
1033                 list_add(&path->list, &codec->dapm_paths);
1034                 list_add(&path->list_sink, &wsink->sources);
1035                 list_add(&path->list_source, &wsource->sinks);
1036                 path->connect = 1;
1037                 return 0;
1038         case snd_soc_dapm_mux:
1039         case snd_soc_dapm_value_mux:
1040                 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1041                         &wsink->kcontrols[0]);
1042                 if (ret != 0)
1043                         goto err;
1044                 break;
1045         case snd_soc_dapm_switch:
1046         case snd_soc_dapm_mixer:
1047         case snd_soc_dapm_mixer_named_ctl:
1048                 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1049                 if (ret != 0)
1050                         goto err;
1051                 break;
1052         case snd_soc_dapm_hp:
1053         case snd_soc_dapm_mic:
1054         case snd_soc_dapm_line:
1055         case snd_soc_dapm_spk:
1056                 list_add(&path->list, &codec->dapm_paths);
1057                 list_add(&path->list_sink, &wsink->sources);
1058                 list_add(&path->list_source, &wsource->sinks);
1059                 path->connect = 0;
1060                 return 0;
1061         }
1062         return 0;
1063
1064 err:
1065         printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1066                 control, sink);
1067         kfree(path);
1068         return ret;
1069 }
1070
1071 /**
1072  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1073  * @codec: codec
1074  * @route: audio routes
1075  * @num: number of routes
1076  *
1077  * Connects 2 dapm widgets together via a named audio path. The sink is
1078  * the widget receiving the audio signal, whilst the source is the sender
1079  * of the audio signal.
1080  *
1081  * Returns 0 for success else error. On error all resources can be freed
1082  * with a call to snd_soc_card_free().
1083  */
1084 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1085                             const struct snd_soc_dapm_route *route, int num)
1086 {
1087         int i, ret;
1088
1089         for (i = 0; i < num; i++) {
1090                 ret = snd_soc_dapm_add_route(codec, route->sink,
1091                                              route->control, route->source);
1092                 if (ret < 0) {
1093                         printk(KERN_ERR "Failed to add route %s->%s\n",
1094                                route->source,
1095                                route->sink);
1096                         return ret;
1097                 }
1098                 route++;
1099         }
1100
1101         return 0;
1102 }
1103 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1104
1105 /**
1106  * snd_soc_dapm_new_widgets - add new dapm widgets
1107  * @codec: audio codec
1108  *
1109  * Checks the codec for any new dapm widgets and creates them if found.
1110  *
1111  * Returns 0 for success.
1112  */
1113 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1114 {
1115         struct snd_soc_dapm_widget *w;
1116
1117         list_for_each_entry(w, &codec->dapm_widgets, list)
1118         {
1119                 if (w->new)
1120                         continue;
1121
1122                 switch(w->id) {
1123                 case snd_soc_dapm_switch:
1124                 case snd_soc_dapm_mixer:
1125                 case snd_soc_dapm_mixer_named_ctl:
1126                         dapm_new_mixer(codec, w);
1127                         break;
1128                 case snd_soc_dapm_mux:
1129                 case snd_soc_dapm_value_mux:
1130                         dapm_new_mux(codec, w);
1131                         break;
1132                 case snd_soc_dapm_adc:
1133                 case snd_soc_dapm_dac:
1134                 case snd_soc_dapm_pga:
1135                         dapm_new_pga(codec, w);
1136                         break;
1137                 case snd_soc_dapm_input:
1138                 case snd_soc_dapm_output:
1139                 case snd_soc_dapm_micbias:
1140                 case snd_soc_dapm_spk:
1141                 case snd_soc_dapm_hp:
1142                 case snd_soc_dapm_mic:
1143                 case snd_soc_dapm_line:
1144                 case snd_soc_dapm_vmid:
1145                 case snd_soc_dapm_pre:
1146                 case snd_soc_dapm_post:
1147                         break;
1148                 }
1149                 w->new = 1;
1150         }
1151
1152         dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1153         return 0;
1154 }
1155 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1156
1157 /**
1158  * snd_soc_dapm_get_volsw - dapm mixer get callback
1159  * @kcontrol: mixer control
1160  * @ucontrol: control element information
1161  *
1162  * Callback to get the value of a dapm mixer control.
1163  *
1164  * Returns 0 for success.
1165  */
1166 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1167         struct snd_ctl_elem_value *ucontrol)
1168 {
1169         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1170         struct soc_mixer_control *mc =
1171                 (struct soc_mixer_control *)kcontrol->private_value;
1172         unsigned int reg = mc->reg;
1173         unsigned int shift = mc->shift;
1174         unsigned int rshift = mc->rshift;
1175         int max = mc->max;
1176         unsigned int invert = mc->invert;
1177         unsigned int mask = (1 << fls(max)) - 1;
1178
1179         /* return the saved value if we are powered down */
1180         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1181                 ucontrol->value.integer.value[0] = widget->saved_value;
1182                 return 0;
1183         }
1184
1185         ucontrol->value.integer.value[0] =
1186                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1187         if (shift != rshift)
1188                 ucontrol->value.integer.value[1] =
1189                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1190         if (invert) {
1191                 ucontrol->value.integer.value[0] =
1192                         max - ucontrol->value.integer.value[0];
1193                 if (shift != rshift)
1194                         ucontrol->value.integer.value[1] =
1195                                 max - ucontrol->value.integer.value[1];
1196         }
1197
1198         return 0;
1199 }
1200 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1201
1202 /**
1203  * snd_soc_dapm_put_volsw - dapm mixer set callback
1204  * @kcontrol: mixer control
1205  * @ucontrol: control element information
1206  *
1207  * Callback to set the value of a dapm mixer control.
1208  *
1209  * Returns 0 for success.
1210  */
1211 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1212         struct snd_ctl_elem_value *ucontrol)
1213 {
1214         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1215         struct soc_mixer_control *mc =
1216                 (struct soc_mixer_control *)kcontrol->private_value;
1217         unsigned int reg = mc->reg;
1218         unsigned int shift = mc->shift;
1219         unsigned int rshift = mc->rshift;
1220         int max = mc->max;
1221         unsigned int mask = (1 << fls(max)) - 1;
1222         unsigned int invert = mc->invert;
1223         unsigned short val, val2, val_mask;
1224         int ret;
1225
1226         val = (ucontrol->value.integer.value[0] & mask);
1227
1228         if (invert)
1229                 val = max - val;
1230         val_mask = mask << shift;
1231         val = val << shift;
1232         if (shift != rshift) {
1233                 val2 = (ucontrol->value.integer.value[1] & mask);
1234                 if (invert)
1235                         val2 = max - val2;
1236                 val_mask |= mask << rshift;
1237                 val |= val2 << rshift;
1238         }
1239
1240         mutex_lock(&widget->codec->mutex);
1241         widget->value = val;
1242
1243         /* save volume value if the widget is powered down */
1244         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1245                 widget->saved_value = val;
1246                 mutex_unlock(&widget->codec->mutex);
1247                 return 1;
1248         }
1249
1250         dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1251         if (widget->event) {
1252                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1253                         ret = widget->event(widget, kcontrol,
1254                                                 SND_SOC_DAPM_PRE_REG);
1255                         if (ret < 0) {
1256                                 ret = 1;
1257                                 goto out;
1258                         }
1259                 }
1260                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1261                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1262                         ret = widget->event(widget, kcontrol,
1263                                                 SND_SOC_DAPM_POST_REG);
1264         } else
1265                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1266
1267 out:
1268         mutex_unlock(&widget->codec->mutex);
1269         return ret;
1270 }
1271 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1272
1273 /**
1274  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1275  * @kcontrol: mixer control
1276  * @ucontrol: control element information
1277  *
1278  * Callback to get the value of a dapm enumerated double mixer control.
1279  *
1280  * Returns 0 for success.
1281  */
1282 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1283         struct snd_ctl_elem_value *ucontrol)
1284 {
1285         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1286         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1287         unsigned short val, bitmask;
1288
1289         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1290                 ;
1291         val = snd_soc_read(widget->codec, e->reg);
1292         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1293         if (e->shift_l != e->shift_r)
1294                 ucontrol->value.enumerated.item[1] =
1295                         (val >> e->shift_r) & (bitmask - 1);
1296
1297         return 0;
1298 }
1299 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1300
1301 /**
1302  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1303  * @kcontrol: mixer control
1304  * @ucontrol: control element information
1305  *
1306  * Callback to set the value of a dapm enumerated double mixer control.
1307  *
1308  * Returns 0 for success.
1309  */
1310 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1311         struct snd_ctl_elem_value *ucontrol)
1312 {
1313         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1314         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1315         unsigned short val, mux;
1316         unsigned short mask, bitmask;
1317         int ret = 0;
1318
1319         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1320                 ;
1321         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1322                 return -EINVAL;
1323         mux = ucontrol->value.enumerated.item[0];
1324         val = mux << e->shift_l;
1325         mask = (bitmask - 1) << e->shift_l;
1326         if (e->shift_l != e->shift_r) {
1327                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1328                         return -EINVAL;
1329                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1330                 mask |= (bitmask - 1) << e->shift_r;
1331         }
1332
1333         mutex_lock(&widget->codec->mutex);
1334         widget->value = val;
1335         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1336         if (widget->event) {
1337                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1338                         ret = widget->event(widget,
1339                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1340                         if (ret < 0)
1341                                 goto out;
1342                 }
1343                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1344                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1345                         ret = widget->event(widget,
1346                                 kcontrol, SND_SOC_DAPM_POST_REG);
1347         } else
1348                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1349
1350 out:
1351         mutex_unlock(&widget->codec->mutex);
1352         return ret;
1353 }
1354 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1355
1356 /**
1357  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1358  *                                      callback
1359  * @kcontrol: mixer control
1360  * @ucontrol: control element information
1361  *
1362  * Callback to get the value of a dapm semi enumerated double mixer control.
1363  *
1364  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1365  * used for handling bitfield coded enumeration for example.
1366  *
1367  * Returns 0 for success.
1368  */
1369 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1370         struct snd_ctl_elem_value *ucontrol)
1371 {
1372         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1373         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1374         unsigned short reg_val, val, mux;
1375
1376         reg_val = snd_soc_read(widget->codec, e->reg);
1377         val = (reg_val >> e->shift_l) & e->mask;
1378         for (mux = 0; mux < e->max; mux++) {
1379                 if (val == e->values[mux])
1380                         break;
1381         }
1382         ucontrol->value.enumerated.item[0] = mux;
1383         if (e->shift_l != e->shift_r) {
1384                 val = (reg_val >> e->shift_r) & e->mask;
1385                 for (mux = 0; mux < e->max; mux++) {
1386                         if (val == e->values[mux])
1387                                 break;
1388                 }
1389                 ucontrol->value.enumerated.item[1] = mux;
1390         }
1391
1392         return 0;
1393 }
1394 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1395
1396 /**
1397  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1398  *                                      callback
1399  * @kcontrol: mixer control
1400  * @ucontrol: control element information
1401  *
1402  * Callback to set the value of a dapm semi enumerated double mixer control.
1403  *
1404  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1405  * used for handling bitfield coded enumeration for example.
1406  *
1407  * Returns 0 for success.
1408  */
1409 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1410         struct snd_ctl_elem_value *ucontrol)
1411 {
1412         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1413         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1414         unsigned short val, mux;
1415         unsigned short mask;
1416         int ret = 0;
1417
1418         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1419                 return -EINVAL;
1420         mux = ucontrol->value.enumerated.item[0];
1421         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1422         mask = e->mask << e->shift_l;
1423         if (e->shift_l != e->shift_r) {
1424                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1425                         return -EINVAL;
1426                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1427                 mask |= e->mask << e->shift_r;
1428         }
1429
1430         mutex_lock(&widget->codec->mutex);
1431         widget->value = val;
1432         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1433         if (widget->event) {
1434                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1435                         ret = widget->event(widget,
1436                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1437                         if (ret < 0)
1438                                 goto out;
1439                 }
1440                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1441                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1442                         ret = widget->event(widget,
1443                                 kcontrol, SND_SOC_DAPM_POST_REG);
1444         } else
1445                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1446
1447 out:
1448         mutex_unlock(&widget->codec->mutex);
1449         return ret;
1450 }
1451 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1452
1453 /**
1454  * snd_soc_dapm_info_pin_switch - Info for a pin switch
1455  *
1456  * @kcontrol: mixer control
1457  * @uinfo: control element information
1458  *
1459  * Callback to provide information about a pin switch control.
1460  */
1461 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1462                                  struct snd_ctl_elem_info *uinfo)
1463 {
1464         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1465         uinfo->count = 1;
1466         uinfo->value.integer.min = 0;
1467         uinfo->value.integer.max = 1;
1468
1469         return 0;
1470 }
1471 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1472
1473 /**
1474  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1475  *
1476  * @kcontrol: mixer control
1477  * @ucontrol: Value
1478  */
1479 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1480                                 struct snd_ctl_elem_value *ucontrol)
1481 {
1482         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1483         const char *pin = (const char *)kcontrol->private_value;
1484
1485         mutex_lock(&codec->mutex);
1486
1487         ucontrol->value.integer.value[0] =
1488                 snd_soc_dapm_get_pin_status(codec, pin);
1489
1490         mutex_unlock(&codec->mutex);
1491
1492         return 0;
1493 }
1494 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1495
1496 /**
1497  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1498  *
1499  * @kcontrol: mixer control
1500  * @ucontrol: Value
1501  */
1502 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1503                                 struct snd_ctl_elem_value *ucontrol)
1504 {
1505         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1506         const char *pin = (const char *)kcontrol->private_value;
1507
1508         mutex_lock(&codec->mutex);
1509
1510         if (ucontrol->value.integer.value[0])
1511                 snd_soc_dapm_enable_pin(codec, pin);
1512         else
1513                 snd_soc_dapm_disable_pin(codec, pin);
1514
1515         snd_soc_dapm_sync(codec);
1516
1517         mutex_unlock(&codec->mutex);
1518
1519         return 0;
1520 }
1521 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1522
1523 /**
1524  * snd_soc_dapm_new_control - create new dapm control
1525  * @codec: audio codec
1526  * @widget: widget template
1527  *
1528  * Creates a new dapm control based upon the template.
1529  *
1530  * Returns 0 for success else error.
1531  */
1532 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1533         const struct snd_soc_dapm_widget *widget)
1534 {
1535         struct snd_soc_dapm_widget *w;
1536
1537         if ((w = dapm_cnew_widget(widget)) == NULL)
1538                 return -ENOMEM;
1539
1540         w->codec = codec;
1541         INIT_LIST_HEAD(&w->sources);
1542         INIT_LIST_HEAD(&w->sinks);
1543         INIT_LIST_HEAD(&w->list);
1544         list_add(&w->list, &codec->dapm_widgets);
1545
1546         /* machine layer set ups unconnected pins and insertions */
1547         w->connected = 1;
1548         return 0;
1549 }
1550 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1551
1552 /**
1553  * snd_soc_dapm_new_controls - create new dapm controls
1554  * @codec: audio codec
1555  * @widget: widget array
1556  * @num: number of widgets
1557  *
1558  * Creates new DAPM controls based upon the templates.
1559  *
1560  * Returns 0 for success else error.
1561  */
1562 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1563         const struct snd_soc_dapm_widget *widget,
1564         int num)
1565 {
1566         int i, ret;
1567
1568         for (i = 0; i < num; i++) {
1569                 ret = snd_soc_dapm_new_control(codec, widget);
1570                 if (ret < 0) {
1571                         printk(KERN_ERR
1572                                "ASoC: Failed to create DAPM control %s: %d\n",
1573                                widget->name, ret);
1574                         return ret;
1575                 }
1576                 widget++;
1577         }
1578         return 0;
1579 }
1580 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1581
1582
1583 /**
1584  * snd_soc_dapm_stream_event - send a stream event to the dapm core
1585  * @codec: audio codec
1586  * @stream: stream name
1587  * @event: stream event
1588  *
1589  * Sends a stream event to the dapm core. The core then makes any
1590  * necessary widget power changes.
1591  *
1592  * Returns 0 for success else error.
1593  */
1594 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1595         char *stream, int event)
1596 {
1597         struct snd_soc_dapm_widget *w;
1598
1599         if (stream == NULL)
1600                 return 0;
1601
1602         mutex_lock(&codec->mutex);
1603         list_for_each_entry(w, &codec->dapm_widgets, list)
1604         {
1605                 if (!w->sname)
1606                         continue;
1607                 pr_debug("widget %s\n %s stream %s event %d\n",
1608                          w->name, w->sname, stream, event);
1609                 if (strstr(w->sname, stream)) {
1610                         switch(event) {
1611                         case SND_SOC_DAPM_STREAM_START:
1612                                 w->active = 1;
1613                                 break;
1614                         case SND_SOC_DAPM_STREAM_STOP:
1615                                 w->active = 0;
1616                                 break;
1617                         case SND_SOC_DAPM_STREAM_SUSPEND:
1618                                 if (w->active)
1619                                         w->suspend = 1;
1620                                 w->active = 0;
1621                                 break;
1622                         case SND_SOC_DAPM_STREAM_RESUME:
1623                                 if (w->suspend) {
1624                                         w->active = 1;
1625                                         w->suspend = 0;
1626                                 }
1627                                 break;
1628                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1629                                 break;
1630                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1631                                 break;
1632                         }
1633                 }
1634         }
1635         mutex_unlock(&codec->mutex);
1636
1637         dapm_power_widgets(codec, event);
1638         dump_dapm(codec, __func__);
1639         return 0;
1640 }
1641 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1642
1643 /**
1644  * snd_soc_dapm_set_bias_level - set the bias level for the system
1645  * @socdev: audio device
1646  * @level: level to configure
1647  *
1648  * Configure the bias (power) levels for the SoC audio device.
1649  *
1650  * Returns 0 for success else error.
1651  */
1652 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1653                                 enum snd_soc_bias_level level)
1654 {
1655         struct snd_soc_card *card = socdev->card;
1656         struct snd_soc_codec *codec = socdev->card->codec;
1657         int ret = 0;
1658
1659         if (card->set_bias_level)
1660                 ret = card->set_bias_level(card, level);
1661         if (ret == 0 && codec->set_bias_level)
1662                 ret = codec->set_bias_level(codec, level);
1663
1664         return ret;
1665 }
1666
1667 /**
1668  * snd_soc_dapm_enable_pin - enable pin.
1669  * @codec: SoC codec
1670  * @pin: pin name
1671  *
1672  * Enables input/output pin and it's parents or children widgets iff there is
1673  * a valid audio route and active audio stream.
1674  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1675  * do any widget power switching.
1676  */
1677 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
1678 {
1679         return snd_soc_dapm_set_pin(codec, pin, 1);
1680 }
1681 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1682
1683 /**
1684  * snd_soc_dapm_disable_pin - disable pin.
1685  * @codec: SoC codec
1686  * @pin: pin name
1687  *
1688  * Disables input/output pin and it's parents or children widgets.
1689  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1690  * do any widget power switching.
1691  */
1692 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
1693 {
1694         return snd_soc_dapm_set_pin(codec, pin, 0);
1695 }
1696 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1697
1698 /**
1699  * snd_soc_dapm_nc_pin - permanently disable pin.
1700  * @codec: SoC codec
1701  * @pin: pin name
1702  *
1703  * Marks the specified pin as being not connected, disabling it along
1704  * any parent or child widgets.  At present this is identical to
1705  * snd_soc_dapm_disable_pin() but in future it will be extended to do
1706  * additional things such as disabling controls which only affect
1707  * paths through the pin.
1708  *
1709  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1710  * do any widget power switching.
1711  */
1712 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
1713 {
1714         return snd_soc_dapm_set_pin(codec, pin, 0);
1715 }
1716 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1717
1718 /**
1719  * snd_soc_dapm_get_pin_status - get audio pin status
1720  * @codec: audio codec
1721  * @pin: audio signal pin endpoint (or start point)
1722  *
1723  * Get audio pin status - connected or disconnected.
1724  *
1725  * Returns 1 for connected otherwise 0.
1726  */
1727 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
1728 {
1729         struct snd_soc_dapm_widget *w;
1730
1731         list_for_each_entry(w, &codec->dapm_widgets, list) {
1732                 if (!strcmp(w->name, pin))
1733                         return w->connected;
1734         }
1735
1736         return 0;
1737 }
1738 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1739
1740 /**
1741  * snd_soc_dapm_free - free dapm resources
1742  * @socdev: SoC device
1743  *
1744  * Free all dapm widgets and resources.
1745  */
1746 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1747 {
1748         struct snd_soc_codec *codec = socdev->card->codec;
1749
1750         snd_soc_dapm_sys_remove(socdev->dev);
1751         dapm_free_widgets(codec);
1752 }
1753 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1754
1755 /* Module information */
1756 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1757 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1758 MODULE_LICENSE("GPL");