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