ASoC: Sort DAPM sequences by CODEC as well
[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  *      DACs/ADCs.
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 <linux/debugfs.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc-dapm.h>
45 #include <sound/initval.h>
46
47 /* debug */
48 #ifdef DEBUG
49 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
50 #else
51 #define dump_dapm(codec, action)
52 #endif
53
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq[] = {
56         [snd_soc_dapm_pre] = 0,
57         [snd_soc_dapm_supply] = 1,
58         [snd_soc_dapm_micbias] = 2,
59         [snd_soc_dapm_aif_in] = 3,
60         [snd_soc_dapm_aif_out] = 3,
61         [snd_soc_dapm_mic] = 4,
62         [snd_soc_dapm_mux] = 5,
63         [snd_soc_dapm_value_mux] = 5,
64         [snd_soc_dapm_dac] = 6,
65         [snd_soc_dapm_mixer] = 7,
66         [snd_soc_dapm_mixer_named_ctl] = 7,
67         [snd_soc_dapm_pga] = 8,
68         [snd_soc_dapm_adc] = 9,
69         [snd_soc_dapm_hp] = 10,
70         [snd_soc_dapm_spk] = 10,
71         [snd_soc_dapm_post] = 11,
72 };
73
74 static int dapm_down_seq[] = {
75         [snd_soc_dapm_pre] = 0,
76         [snd_soc_dapm_adc] = 1,
77         [snd_soc_dapm_hp] = 2,
78         [snd_soc_dapm_spk] = 2,
79         [snd_soc_dapm_pga] = 4,
80         [snd_soc_dapm_mixer_named_ctl] = 5,
81         [snd_soc_dapm_mixer] = 5,
82         [snd_soc_dapm_dac] = 6,
83         [snd_soc_dapm_mic] = 7,
84         [snd_soc_dapm_micbias] = 8,
85         [snd_soc_dapm_mux] = 9,
86         [snd_soc_dapm_value_mux] = 9,
87         [snd_soc_dapm_aif_in] = 10,
88         [snd_soc_dapm_aif_out] = 10,
89         [snd_soc_dapm_supply] = 11,
90         [snd_soc_dapm_post] = 12,
91 };
92
93 static void pop_wait(u32 pop_time)
94 {
95         if (pop_time)
96                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97 }
98
99 static void pop_dbg(u32 pop_time, const char *fmt, ...)
100 {
101         va_list args;
102
103         va_start(args, fmt);
104
105         if (pop_time) {
106                 vprintk(fmt, args);
107                 pop_wait(pop_time);
108         }
109
110         va_end(args);
111 }
112
113 /* create a new dapm widget */
114 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
115         const struct snd_soc_dapm_widget *_widget)
116 {
117         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
118 }
119
120 /**
121  * snd_soc_dapm_set_bias_level - set the bias level for the system
122  * @socdev: audio device
123  * @level: level to configure
124  *
125  * Configure the bias (power) levels for the SoC audio device.
126  *
127  * Returns 0 for success else error.
128  */
129 static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
130                                        enum snd_soc_bias_level level)
131 {
132         struct snd_soc_card *card = socdev->card;
133         struct snd_soc_codec *codec = socdev->card->codec;
134         int ret = 0;
135
136         switch (level) {
137         case SND_SOC_BIAS_ON:
138                 dev_dbg(socdev->dev, "Setting full bias\n");
139                 break;
140         case SND_SOC_BIAS_PREPARE:
141                 dev_dbg(socdev->dev, "Setting bias prepare\n");
142                 break;
143         case SND_SOC_BIAS_STANDBY:
144                 dev_dbg(socdev->dev, "Setting standby bias\n");
145                 break;
146         case SND_SOC_BIAS_OFF:
147                 dev_dbg(socdev->dev, "Setting bias off\n");
148                 break;
149         default:
150                 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
151                 return -EINVAL;
152         }
153
154         if (card->set_bias_level)
155                 ret = card->set_bias_level(card, level);
156         if (ret == 0) {
157                 if (codec->set_bias_level)
158                         ret = codec->set_bias_level(codec, level);
159                 else
160                         codec->bias_level = level;
161         }
162
163         return ret;
164 }
165
166 /* set up initial codec paths */
167 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
168         struct snd_soc_dapm_path *p, int i)
169 {
170         switch (w->id) {
171         case snd_soc_dapm_switch:
172         case snd_soc_dapm_mixer:
173         case snd_soc_dapm_mixer_named_ctl: {
174                 int val;
175                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
176                         w->kcontrols[i].private_value;
177                 unsigned int reg = mc->reg;
178                 unsigned int shift = mc->shift;
179                 int max = mc->max;
180                 unsigned int mask = (1 << fls(max)) - 1;
181                 unsigned int invert = mc->invert;
182
183                 val = snd_soc_read(w->codec, reg);
184                 val = (val >> shift) & mask;
185
186                 if ((invert && !val) || (!invert && val))
187                         p->connect = 1;
188                 else
189                         p->connect = 0;
190         }
191         break;
192         case snd_soc_dapm_mux: {
193                 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
194                 int val, item, bitmask;
195
196                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
197                 ;
198                 val = snd_soc_read(w->codec, e->reg);
199                 item = (val >> e->shift_l) & (bitmask - 1);
200
201                 p->connect = 0;
202                 for (i = 0; i < e->max; i++) {
203                         if (!(strcmp(p->name, e->texts[i])) && item == i)
204                                 p->connect = 1;
205                 }
206         }
207         break;
208         case snd_soc_dapm_value_mux: {
209                 struct soc_enum *e = (struct soc_enum *)
210                         w->kcontrols[i].private_value;
211                 int val, item;
212
213                 val = snd_soc_read(w->codec, e->reg);
214                 val = (val >> e->shift_l) & e->mask;
215                 for (item = 0; item < e->max; item++) {
216                         if (val == e->values[item])
217                                 break;
218                 }
219
220                 p->connect = 0;
221                 for (i = 0; i < e->max; i++) {
222                         if (!(strcmp(p->name, e->texts[i])) && item == i)
223                                 p->connect = 1;
224                 }
225         }
226         break;
227         /* does not effect routing - always connected */
228         case snd_soc_dapm_pga:
229         case snd_soc_dapm_output:
230         case snd_soc_dapm_adc:
231         case snd_soc_dapm_input:
232         case snd_soc_dapm_dac:
233         case snd_soc_dapm_micbias:
234         case snd_soc_dapm_vmid:
235         case snd_soc_dapm_supply:
236         case snd_soc_dapm_aif_in:
237         case snd_soc_dapm_aif_out:
238                 p->connect = 1;
239         break;
240         /* does effect routing - dynamically connected */
241         case snd_soc_dapm_hp:
242         case snd_soc_dapm_mic:
243         case snd_soc_dapm_spk:
244         case snd_soc_dapm_line:
245         case snd_soc_dapm_pre:
246         case snd_soc_dapm_post:
247                 p->connect = 0;
248         break;
249         }
250 }
251
252 /* connect mux widget to its interconnecting audio paths */
253 static int dapm_connect_mux(struct snd_soc_codec *codec,
254         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
255         struct snd_soc_dapm_path *path, const char *control_name,
256         const struct snd_kcontrol_new *kcontrol)
257 {
258         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
259         int i;
260
261         for (i = 0; i < e->max; i++) {
262                 if (!(strcmp(control_name, e->texts[i]))) {
263                         list_add(&path->list, &codec->dapm_paths);
264                         list_add(&path->list_sink, &dest->sources);
265                         list_add(&path->list_source, &src->sinks);
266                         path->name = (char*)e->texts[i];
267                         dapm_set_path_status(dest, path, 0);
268                         return 0;
269                 }
270         }
271
272         return -ENODEV;
273 }
274
275 /* connect mixer widget to its interconnecting audio paths */
276 static int dapm_connect_mixer(struct snd_soc_codec *codec,
277         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
278         struct snd_soc_dapm_path *path, const char *control_name)
279 {
280         int i;
281
282         /* search for mixer kcontrol */
283         for (i = 0; i < dest->num_kcontrols; i++) {
284                 if (!strcmp(control_name, dest->kcontrols[i].name)) {
285                         list_add(&path->list, &codec->dapm_paths);
286                         list_add(&path->list_sink, &dest->sources);
287                         list_add(&path->list_source, &src->sinks);
288                         path->name = dest->kcontrols[i].name;
289                         dapm_set_path_status(dest, path, i);
290                         return 0;
291                 }
292         }
293         return -ENODEV;
294 }
295
296 /* update dapm codec register bits */
297 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
298 {
299         int change, power;
300         unsigned int old, new;
301         struct snd_soc_codec *codec = widget->codec;
302
303         /* check for valid widgets */
304         if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
305                 widget->id == snd_soc_dapm_output ||
306                 widget->id == snd_soc_dapm_hp ||
307                 widget->id == snd_soc_dapm_mic ||
308                 widget->id == snd_soc_dapm_line ||
309                 widget->id == snd_soc_dapm_spk)
310                 return 0;
311
312         power = widget->power;
313         if (widget->invert)
314                 power = (power ? 0:1);
315
316         old = snd_soc_read(codec, widget->reg);
317         new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
318
319         change = old != new;
320         if (change) {
321                 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
322                         widget->name, widget->power ? "on" : "off",
323                         codec->pop_time);
324                 snd_soc_write(codec, widget->reg, new);
325                 pop_wait(codec->pop_time);
326         }
327         pr_debug("reg %x old %x new %x change %d\n", widget->reg,
328                  old, new, change);
329         return change;
330 }
331
332 /* ramps the volume up or down to minimise pops before or after a
333  * DAPM power event */
334 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
335 {
336         const struct snd_kcontrol_new *k = widget->kcontrols;
337
338         if (widget->muted && !power)
339                 return 0;
340         if (!widget->muted && power)
341                 return 0;
342
343         if (widget->num_kcontrols && k) {
344                 struct soc_mixer_control *mc =
345                         (struct soc_mixer_control *)k->private_value;
346                 unsigned int reg = mc->reg;
347                 unsigned int shift = mc->shift;
348                 int max = mc->max;
349                 unsigned int mask = (1 << fls(max)) - 1;
350                 unsigned int invert = mc->invert;
351
352                 if (power) {
353                         int i;
354                         /* power up has happended, increase volume to last level */
355                         if (invert) {
356                                 for (i = max; i > widget->saved_value; i--)
357                                         snd_soc_update_bits(widget->codec, reg, mask, i);
358                         } else {
359                                 for (i = 0; i < widget->saved_value; i++)
360                                         snd_soc_update_bits(widget->codec, reg, mask, i);
361                         }
362                         widget->muted = 0;
363                 } else {
364                         /* power down is about to occur, decrease volume to mute */
365                         int val = snd_soc_read(widget->codec, reg);
366                         int i = widget->saved_value = (val >> shift) & mask;
367                         if (invert) {
368                                 for (; i < mask; i++)
369                                         snd_soc_update_bits(widget->codec, reg, mask, i);
370                         } else {
371                                 for (; i > 0; i--)
372                                         snd_soc_update_bits(widget->codec, reg, mask, i);
373                         }
374                         widget->muted = 1;
375                 }
376         }
377         return 0;
378 }
379
380 /* create new dapm mixer control */
381 static int dapm_new_mixer(struct snd_soc_codec *codec,
382         struct snd_soc_dapm_widget *w)
383 {
384         int i, ret = 0;
385         size_t name_len;
386         struct snd_soc_dapm_path *path;
387
388         /* add kcontrol */
389         for (i = 0; i < w->num_kcontrols; i++) {
390
391                 /* match name */
392                 list_for_each_entry(path, &w->sources, list_sink) {
393
394                         /* mixer/mux paths name must match control name */
395                         if (path->name != (char*)w->kcontrols[i].name)
396                                 continue;
397
398                         /* add dapm control with long name.
399                          * for dapm_mixer this is the concatenation of the
400                          * mixer and kcontrol name.
401                          * for dapm_mixer_named_ctl this is simply the
402                          * kcontrol name.
403                          */
404                         name_len = strlen(w->kcontrols[i].name) + 1;
405                         if (w->id != snd_soc_dapm_mixer_named_ctl)
406                                 name_len += 1 + strlen(w->name);
407
408                         path->long_name = kmalloc(name_len, GFP_KERNEL);
409
410                         if (path->long_name == NULL)
411                                 return -ENOMEM;
412
413                         switch (w->id) {
414                         default:
415                                 snprintf(path->long_name, name_len, "%s %s",
416                                          w->name, w->kcontrols[i].name);
417                                 break;
418                         case snd_soc_dapm_mixer_named_ctl:
419                                 snprintf(path->long_name, name_len, "%s",
420                                          w->kcontrols[i].name);
421                                 break;
422                         }
423
424                         path->long_name[name_len - 1] = '\0';
425
426                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
427                                 path->long_name);
428                         ret = snd_ctl_add(codec->card, path->kcontrol);
429                         if (ret < 0) {
430                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
431                                        path->long_name,
432                                        ret);
433                                 kfree(path->long_name);
434                                 path->long_name = NULL;
435                                 return ret;
436                         }
437                 }
438         }
439         return ret;
440 }
441
442 /* create new dapm mux control */
443 static int dapm_new_mux(struct snd_soc_codec *codec,
444         struct snd_soc_dapm_widget *w)
445 {
446         struct snd_soc_dapm_path *path = NULL;
447         struct snd_kcontrol *kcontrol;
448         int ret = 0;
449
450         if (!w->num_kcontrols) {
451                 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
452                 return -EINVAL;
453         }
454
455         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
456         ret = snd_ctl_add(codec->card, kcontrol);
457         if (ret < 0)
458                 goto err;
459
460         list_for_each_entry(path, &w->sources, list_sink)
461                 path->kcontrol = kcontrol;
462
463         return ret;
464
465 err:
466         printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
467         return ret;
468 }
469
470 /* create new dapm volume control */
471 static int dapm_new_pga(struct snd_soc_codec *codec,
472         struct snd_soc_dapm_widget *w)
473 {
474         struct snd_kcontrol *kcontrol;
475         int ret = 0;
476
477         if (!w->num_kcontrols)
478                 return -EINVAL;
479
480         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
481         ret = snd_ctl_add(codec->card, kcontrol);
482         if (ret < 0) {
483                 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
484                 return ret;
485         }
486
487         return ret;
488 }
489
490 /* reset 'walked' bit for each dapm path */
491 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
492 {
493         struct snd_soc_dapm_path *p;
494
495         list_for_each_entry(p, &codec->dapm_paths, list)
496                 p->walked = 0;
497 }
498
499 /*
500  * Recursively check for a completed path to an active or physically connected
501  * output widget. Returns number of complete paths.
502  */
503 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
504 {
505         struct snd_soc_dapm_path *path;
506         int con = 0;
507
508         if (widget->id == snd_soc_dapm_supply)
509                 return 0;
510
511         switch (widget->id) {
512         case snd_soc_dapm_adc:
513         case snd_soc_dapm_aif_out:
514                 if (widget->active)
515                         return 1;
516         default:
517                 break;
518         }
519
520         if (widget->connected) {
521                 /* connected pin ? */
522                 if (widget->id == snd_soc_dapm_output && !widget->ext)
523                         return 1;
524
525                 /* connected jack or spk ? */
526                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
527                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
528                         return 1;
529         }
530
531         list_for_each_entry(path, &widget->sinks, list_source) {
532                 if (path->walked)
533                         continue;
534
535                 if (path->sink && path->connect) {
536                         path->walked = 1;
537                         con += is_connected_output_ep(path->sink);
538                 }
539         }
540
541         return con;
542 }
543
544 /*
545  * Recursively check for a completed path to an active or physically connected
546  * input widget. Returns number of complete paths.
547  */
548 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
549 {
550         struct snd_soc_dapm_path *path;
551         int con = 0;
552
553         if (widget->id == snd_soc_dapm_supply)
554                 return 0;
555
556         /* active stream ? */
557         switch (widget->id) {
558         case snd_soc_dapm_dac:
559         case snd_soc_dapm_aif_in:
560                 if (widget->active)
561                         return 1;
562         default:
563                 break;
564         }
565
566         if (widget->connected) {
567                 /* connected pin ? */
568                 if (widget->id == snd_soc_dapm_input && !widget->ext)
569                         return 1;
570
571                 /* connected VMID/Bias for lower pops */
572                 if (widget->id == snd_soc_dapm_vmid)
573                         return 1;
574
575                 /* connected jack ? */
576                 if (widget->id == snd_soc_dapm_mic ||
577                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
578                         return 1;
579         }
580
581         list_for_each_entry(path, &widget->sources, list_sink) {
582                 if (path->walked)
583                         continue;
584
585                 if (path->source && path->connect) {
586                         path->walked = 1;
587                         con += is_connected_input_ep(path->source);
588                 }
589         }
590
591         return con;
592 }
593
594 /*
595  * Handler for generic register modifier widget.
596  */
597 int dapm_reg_event(struct snd_soc_dapm_widget *w,
598                    struct snd_kcontrol *kcontrol, int event)
599 {
600         unsigned int val;
601
602         if (SND_SOC_DAPM_EVENT_ON(event))
603                 val = w->on_val;
604         else
605                 val = w->off_val;
606
607         snd_soc_update_bits(w->codec, -(w->reg + 1),
608                             w->mask << w->shift, val << w->shift);
609
610         return 0;
611 }
612 EXPORT_SYMBOL_GPL(dapm_reg_event);
613
614 /* Standard power change method, used to apply power changes to most
615  * widgets.
616  */
617 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
618 {
619         int ret;
620
621         /* call any power change event handlers */
622         if (w->event)
623                 pr_debug("power %s event for %s flags %x\n",
624                          w->power ? "on" : "off",
625                          w->name, w->event_flags);
626
627         /* power up pre event */
628         if (w->power && w->event &&
629             (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
630                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
631                 if (ret < 0)
632                         return ret;
633         }
634
635         /* power down pre event */
636         if (!w->power && w->event &&
637             (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
638                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
639                 if (ret < 0)
640                         return ret;
641         }
642
643         /* Lower PGA volume to reduce pops */
644         if (w->id == snd_soc_dapm_pga && !w->power)
645                 dapm_set_pga(w, w->power);
646
647         dapm_update_bits(w);
648
649         /* Raise PGA volume to reduce pops */
650         if (w->id == snd_soc_dapm_pga && w->power)
651                 dapm_set_pga(w, w->power);
652
653         /* power up post event */
654         if (w->power && w->event &&
655             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
656                 ret = w->event(w,
657                                NULL, SND_SOC_DAPM_POST_PMU);
658                 if (ret < 0)
659                         return ret;
660         }
661
662         /* power down post event */
663         if (!w->power && w->event &&
664             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
665                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
666                 if (ret < 0)
667                         return ret;
668         }
669
670         return 0;
671 }
672
673 /* Generic check to see if a widget should be powered.
674  */
675 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
676 {
677         int in, out;
678
679         in = is_connected_input_ep(w);
680         dapm_clear_walk(w->codec);
681         out = is_connected_output_ep(w);
682         dapm_clear_walk(w->codec);
683         return out != 0 && in != 0;
684 }
685
686 /* Check to see if an ADC has power */
687 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
688 {
689         int in;
690
691         if (w->active) {
692                 in = is_connected_input_ep(w);
693                 dapm_clear_walk(w->codec);
694                 return in != 0;
695         } else {
696                 return dapm_generic_check_power(w);
697         }
698 }
699
700 /* Check to see if a DAC has power */
701 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
702 {
703         int out;
704
705         if (w->active) {
706                 out = is_connected_output_ep(w);
707                 dapm_clear_walk(w->codec);
708                 return out != 0;
709         } else {
710                 return dapm_generic_check_power(w);
711         }
712 }
713
714 /* Check to see if a power supply is needed */
715 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
716 {
717         struct snd_soc_dapm_path *path;
718         int power = 0;
719
720         /* Check if one of our outputs is connected */
721         list_for_each_entry(path, &w->sinks, list_source) {
722                 if (path->connected &&
723                     !path->connected(path->source, path->sink))
724                         continue;
725
726                 if (path->sink && path->sink->power_check &&
727                     path->sink->power_check(path->sink)) {
728                         power = 1;
729                         break;
730                 }
731         }
732
733         dapm_clear_walk(w->codec);
734
735         return power;
736 }
737
738 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
739                             struct snd_soc_dapm_widget *b,
740                             int sort[])
741 {
742         if (a->codec != b->codec)
743                 return (unsigned long)a - (unsigned long)b;
744         if (sort[a->id] != sort[b->id])
745                 return sort[a->id] - sort[b->id];
746         if (a->reg != b->reg)
747                 return a->reg - b->reg;
748
749         return 0;
750 }
751
752 /* Insert a widget in order into a DAPM power sequence. */
753 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
754                             struct list_head *list,
755                             int sort[])
756 {
757         struct snd_soc_dapm_widget *w;
758
759         list_for_each_entry(w, list, power_list)
760                 if (dapm_seq_compare(new_widget, w, sort) < 0) {
761                         list_add_tail(&new_widget->power_list, &w->power_list);
762                         return;
763                 }
764
765         list_add_tail(&new_widget->power_list, list);
766 }
767
768 /* Apply the coalesced changes from a DAPM sequence */
769 static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
770                                    struct list_head *pending)
771 {
772         struct snd_soc_dapm_widget *w;
773         int reg, power, ret;
774         unsigned int value = 0;
775         unsigned int mask = 0;
776         unsigned int cur_mask;
777
778         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
779                                power_list)->reg;
780
781         list_for_each_entry(w, pending, power_list) {
782                 cur_mask = 1 << w->shift;
783                 BUG_ON(reg != w->reg);
784
785                 if (w->invert)
786                         power = !w->power;
787                 else
788                         power = w->power;
789
790                 mask |= cur_mask;
791                 if (power)
792                         value |= cur_mask;
793
794                 pop_dbg(codec->pop_time,
795                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
796                         w->name, reg, value, mask);
797
798                 /* power up pre event */
799                 if (w->power && w->event &&
800                     (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
801                         pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
802                                 w->name);
803                         ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
804                         if (ret < 0)
805                                 pr_err("%s: pre event failed: %d\n",
806                                        w->name, ret);
807                 }
808
809                 /* power down pre event */
810                 if (!w->power && w->event &&
811                     (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
812                         pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
813                                 w->name);
814                         ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
815                         if (ret < 0)
816                                 pr_err("%s: pre event failed: %d\n",
817                                        w->name, ret);
818                 }
819
820                 /* Lower PGA volume to reduce pops */
821                 if (w->id == snd_soc_dapm_pga && !w->power)
822                         dapm_set_pga(w, w->power);
823         }
824
825         if (reg >= 0) {
826                 pop_dbg(codec->pop_time,
827                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
828                         value, mask, reg, codec->pop_time);
829                 pop_wait(codec->pop_time);
830                 snd_soc_update_bits(codec, reg, mask, value);
831         }
832
833         list_for_each_entry(w, pending, power_list) {
834                 /* Raise PGA volume to reduce pops */
835                 if (w->id == snd_soc_dapm_pga && w->power)
836                         dapm_set_pga(w, w->power);
837
838                 /* power up post event */
839                 if (w->power && w->event &&
840                     (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
841                         pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
842                                 w->name);
843                         ret = w->event(w,
844                                        NULL, SND_SOC_DAPM_POST_PMU);
845                         if (ret < 0)
846                                 pr_err("%s: post event failed: %d\n",
847                                        w->name, ret);
848                 }
849
850                 /* power down post event */
851                 if (!w->power && w->event &&
852                     (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
853                         pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
854                                 w->name);
855                         ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
856                         if (ret < 0)
857                                 pr_err("%s: post event failed: %d\n",
858                                        w->name, ret);
859                 }
860         }
861 }
862
863 /* Apply a DAPM power sequence.
864  *
865  * We walk over a pre-sorted list of widgets to apply power to.  In
866  * order to minimise the number of writes to the device required
867  * multiple widgets will be updated in a single write where possible.
868  * Currently anything that requires more than a single write is not
869  * handled.
870  */
871 static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
872                          int event, int sort[])
873 {
874         struct snd_soc_dapm_widget *w, *n;
875         LIST_HEAD(pending);
876         int cur_sort = -1;
877         int cur_reg = SND_SOC_NOPM;
878         int ret;
879
880         list_for_each_entry_safe(w, n, list, power_list) {
881                 ret = 0;
882
883                 /* Do we need to apply any queued changes? */
884                 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
885                         if (!list_empty(&pending))
886                                 dapm_seq_run_coalesced(codec, &pending);
887
888                         INIT_LIST_HEAD(&pending);
889                         cur_sort = -1;
890                         cur_reg = SND_SOC_NOPM;
891                 }
892
893                 switch (w->id) {
894                 case snd_soc_dapm_pre:
895                         if (!w->event)
896                                 list_for_each_entry_safe_continue(w, n, list,
897                                                                   power_list);
898
899                         if (event == SND_SOC_DAPM_STREAM_START)
900                                 ret = w->event(w,
901                                                NULL, SND_SOC_DAPM_PRE_PMU);
902                         else if (event == SND_SOC_DAPM_STREAM_STOP)
903                                 ret = w->event(w,
904                                                NULL, SND_SOC_DAPM_PRE_PMD);
905                         break;
906
907                 case snd_soc_dapm_post:
908                         if (!w->event)
909                                 list_for_each_entry_safe_continue(w, n, list,
910                                                                   power_list);
911
912                         if (event == SND_SOC_DAPM_STREAM_START)
913                                 ret = w->event(w,
914                                                NULL, SND_SOC_DAPM_POST_PMU);
915                         else if (event == SND_SOC_DAPM_STREAM_STOP)
916                                 ret = w->event(w,
917                                                NULL, SND_SOC_DAPM_POST_PMD);
918                         break;
919
920                 case snd_soc_dapm_input:
921                 case snd_soc_dapm_output:
922                 case snd_soc_dapm_hp:
923                 case snd_soc_dapm_mic:
924                 case snd_soc_dapm_line:
925                 case snd_soc_dapm_spk:
926                         /* No register support currently */
927                         ret = dapm_generic_apply_power(w);
928                         break;
929
930                 default:
931                         /* Queue it up for application */
932                         cur_sort = sort[w->id];
933                         cur_reg = w->reg;
934                         list_move(&w->power_list, &pending);
935                         break;
936                 }
937
938                 if (ret < 0)
939                         pr_err("Failed to apply widget power: %d\n",
940                                ret);
941         }
942
943         if (!list_empty(&pending))
944                 dapm_seq_run_coalesced(codec, &pending);
945 }
946
947 /*
948  * Scan each dapm widget for complete audio path.
949  * A complete path is a route that has valid endpoints i.e.:-
950  *
951  *  o DAC to output pin.
952  *  o Input Pin to ADC.
953  *  o Input pin to Output pin (bypass, sidetone)
954  *  o DAC to ADC (loopback).
955  */
956 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
957 {
958         struct snd_soc_device *socdev = codec->socdev;
959         struct snd_soc_dapm_widget *w;
960         LIST_HEAD(up_list);
961         LIST_HEAD(down_list);
962         int ret = 0;
963         int power;
964         int sys_power = 0;
965
966         /* Check which widgets we need to power and store them in
967          * lists indicating if they should be powered up or down.
968          */
969         list_for_each_entry(w, &codec->dapm_widgets, list) {
970                 switch (w->id) {
971                 case snd_soc_dapm_pre:
972                         dapm_seq_insert(w, &down_list, dapm_down_seq);
973                         break;
974                 case snd_soc_dapm_post:
975                         dapm_seq_insert(w, &up_list, dapm_up_seq);
976                         break;
977
978                 default:
979                         if (!w->power_check)
980                                 continue;
981
982                         /* If we're suspending then pull down all the 
983                          * power. */
984                         switch (event) {
985                         case SND_SOC_DAPM_STREAM_SUSPEND:
986                                 power = 0;
987                                 break;
988
989                         default:
990                                 power = w->power_check(w);
991                                 if (power)
992                                         sys_power = 1;
993                                 break;
994                         }
995
996                         if (w->power == power)
997                                 continue;
998
999                         if (power)
1000                                 dapm_seq_insert(w, &up_list, dapm_up_seq);
1001                         else
1002                                 dapm_seq_insert(w, &down_list, dapm_down_seq);
1003
1004                         w->power = power;
1005                         break;
1006                 }
1007         }
1008
1009         /* If there are no DAPM widgets then try to figure out power from the
1010          * event type.
1011          */
1012         if (list_empty(&codec->dapm_widgets)) {
1013                 switch (event) {
1014                 case SND_SOC_DAPM_STREAM_START:
1015                 case SND_SOC_DAPM_STREAM_RESUME:
1016                         sys_power = 1;
1017                         break;
1018                 case SND_SOC_DAPM_STREAM_SUSPEND:
1019                         sys_power = 0;
1020                         break;
1021                 case SND_SOC_DAPM_STREAM_NOP:
1022                         sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
1023                         break;
1024                 default:
1025                         break;
1026                 }
1027         }
1028
1029         /* If we're changing to all on or all off then prepare */
1030         if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
1031             (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
1032                 ret = snd_soc_dapm_set_bias_level(socdev,
1033                                                   SND_SOC_BIAS_PREPARE);
1034                 if (ret != 0)
1035                         pr_err("Failed to prepare bias: %d\n", ret);
1036         }
1037
1038         /* Power down widgets first; try to avoid amplifying pops. */
1039         dapm_seq_run(codec, &down_list, event, dapm_down_seq);
1040
1041         /* Now power up. */
1042         dapm_seq_run(codec, &up_list, event, dapm_up_seq);
1043
1044         /* If we just powered the last thing off drop to standby bias */
1045         if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1046                 ret = snd_soc_dapm_set_bias_level(socdev,
1047                                                   SND_SOC_BIAS_STANDBY);
1048                 if (ret != 0)
1049                         pr_err("Failed to apply standby bias: %d\n", ret);
1050         }
1051
1052         /* If we just powered up then move to active bias */
1053         if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1054                 ret = snd_soc_dapm_set_bias_level(socdev,
1055                                                   SND_SOC_BIAS_ON);
1056                 if (ret != 0)
1057                         pr_err("Failed to apply active bias: %d\n", ret);
1058         }
1059
1060         pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1061                 codec->pop_time);
1062
1063         return 0;
1064 }
1065
1066 #ifdef DEBUG
1067 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
1068 {
1069         struct snd_soc_dapm_widget *w;
1070         struct snd_soc_dapm_path *p = NULL;
1071         int in, out;
1072
1073         printk("DAPM %s %s\n", codec->name, action);
1074
1075         list_for_each_entry(w, &codec->dapm_widgets, list) {
1076
1077                 /* only display widgets that effect routing */
1078                 switch (w->id) {
1079                 case snd_soc_dapm_pre:
1080                 case snd_soc_dapm_post:
1081                 case snd_soc_dapm_vmid:
1082                         continue;
1083                 case snd_soc_dapm_mux:
1084                 case snd_soc_dapm_value_mux:
1085                 case snd_soc_dapm_output:
1086                 case snd_soc_dapm_input:
1087                 case snd_soc_dapm_switch:
1088                 case snd_soc_dapm_hp:
1089                 case snd_soc_dapm_mic:
1090                 case snd_soc_dapm_spk:
1091                 case snd_soc_dapm_line:
1092                 case snd_soc_dapm_micbias:
1093                 case snd_soc_dapm_dac:
1094                 case snd_soc_dapm_adc:
1095                 case snd_soc_dapm_pga:
1096                 case snd_soc_dapm_mixer:
1097                 case snd_soc_dapm_mixer_named_ctl:
1098                 case snd_soc_dapm_supply:
1099                 case snd_soc_dapm_aif_in:
1100                 case snd_soc_dapm_aif_out:
1101                         if (w->name) {
1102                                 in = is_connected_input_ep(w);
1103                                 dapm_clear_walk(w->codec);
1104                                 out = is_connected_output_ep(w);
1105                                 dapm_clear_walk(w->codec);
1106                                 printk("%s: %s  in %d out %d\n", w->name,
1107                                         w->power ? "On":"Off",in, out);
1108
1109                                 list_for_each_entry(p, &w->sources, list_sink) {
1110                                         if (p->connect)
1111                                                 printk(" in  %s %s\n", p->name ? p->name : "static",
1112                                                         p->source->name);
1113                                 }
1114                                 list_for_each_entry(p, &w->sinks, list_source) {
1115                                         if (p->connect)
1116                                                 printk(" out %s %s\n", p->name ? p->name : "static",
1117                                                         p->sink->name);
1118                                 }
1119                         }
1120                 break;
1121                 }
1122         }
1123 }
1124 #endif
1125
1126 #ifdef CONFIG_DEBUG_FS
1127 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1128 {
1129         file->private_data = inode->i_private;
1130         return 0;
1131 }
1132
1133 static ssize_t dapm_widget_power_read_file(struct file *file,
1134                                            char __user *user_buf,
1135                                            size_t count, loff_t *ppos)
1136 {
1137         struct snd_soc_dapm_widget *w = file->private_data;
1138         char *buf;
1139         int in, out;
1140         ssize_t ret;
1141         struct snd_soc_dapm_path *p = NULL;
1142
1143         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1144         if (!buf)
1145                 return -ENOMEM;
1146
1147         in = is_connected_input_ep(w);
1148         dapm_clear_walk(w->codec);
1149         out = is_connected_output_ep(w);
1150         dapm_clear_walk(w->codec);
1151
1152         ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
1153                        w->name, w->power ? "On" : "Off", in, out);
1154
1155         if (w->reg >= 0)
1156                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1157                                 " - R%d(0x%x) bit %d",
1158                                 w->reg, w->reg, w->shift);
1159
1160         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1161
1162         if (w->sname)
1163                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1164                                 w->sname,
1165                                 w->active ? "active" : "inactive");
1166
1167         list_for_each_entry(p, &w->sources, list_sink) {
1168                 if (p->connected && !p->connected(w, p->sink))
1169                         continue;
1170
1171                 if (p->connect)
1172                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1173                                         " in  %s %s\n",
1174                                         p->name ? p->name : "static",
1175                                         p->source->name);
1176         }
1177         list_for_each_entry(p, &w->sinks, list_source) {
1178                 if (p->connected && !p->connected(w, p->sink))
1179                         continue;
1180
1181                 if (p->connect)
1182                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1183                                         " out %s %s\n",
1184                                         p->name ? p->name : "static",
1185                                         p->sink->name);
1186         }
1187
1188         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1189
1190         kfree(buf);
1191         return ret;
1192 }
1193
1194 static const struct file_operations dapm_widget_power_fops = {
1195         .open = dapm_widget_power_open_file,
1196         .read = dapm_widget_power_read_file,
1197 };
1198
1199 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1200 {
1201         struct snd_soc_dapm_widget *w;
1202         struct dentry *d;
1203
1204         if (!codec->debugfs_dapm)
1205                 return;
1206
1207         list_for_each_entry(w, &codec->dapm_widgets, list) {
1208                 if (!w->name)
1209                         continue;
1210
1211                 d = debugfs_create_file(w->name, 0444,
1212                                         codec->debugfs_dapm, w,
1213                                         &dapm_widget_power_fops);
1214                 if (!d)
1215                         printk(KERN_WARNING
1216                                "ASoC: Failed to create %s debugfs file\n",
1217                                w->name);
1218         }
1219 }
1220 #else
1221 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1222 {
1223 }
1224 #endif
1225
1226 /* test and update the power status of a mux widget */
1227 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1228                                  struct snd_kcontrol *kcontrol, int change,
1229                                  int mux, struct soc_enum *e)
1230 {
1231         struct snd_soc_dapm_path *path;
1232         int found = 0;
1233
1234         if (widget->id != snd_soc_dapm_mux &&
1235             widget->id != snd_soc_dapm_value_mux)
1236                 return -ENODEV;
1237
1238         if (!change)
1239                 return 0;
1240
1241         /* find dapm widget path assoc with kcontrol */
1242         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1243                 if (path->kcontrol != kcontrol)
1244                         continue;
1245
1246                 if (!path->name || !e->texts[mux])
1247                         continue;
1248
1249                 found = 1;
1250                 /* we now need to match the string in the enum to the path */
1251                 if (!(strcmp(path->name, e->texts[mux])))
1252                         path->connect = 1; /* new connection */
1253                 else
1254                         path->connect = 0; /* old connection must be powered down */
1255         }
1256
1257         if (found) {
1258                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1259                 dump_dapm(widget->codec, "mux power update");
1260         }
1261
1262         return 0;
1263 }
1264
1265 /* test and update the power status of a mixer or switch widget */
1266 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1267                                    struct snd_kcontrol *kcontrol, int connect)
1268 {
1269         struct snd_soc_dapm_path *path;
1270         int found = 0;
1271
1272         if (widget->id != snd_soc_dapm_mixer &&
1273             widget->id != snd_soc_dapm_mixer_named_ctl &&
1274             widget->id != snd_soc_dapm_switch)
1275                 return -ENODEV;
1276
1277         /* find dapm widget path assoc with kcontrol */
1278         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1279                 if (path->kcontrol != kcontrol)
1280                         continue;
1281
1282                 /* found, now check type */
1283                 found = 1;
1284                 path->connect = connect;
1285                 break;
1286         }
1287
1288         if (found) {
1289                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1290                 dump_dapm(widget->codec, "mixer power update");
1291         }
1292
1293         return 0;
1294 }
1295
1296 /* show dapm widget status in sys fs */
1297 static ssize_t dapm_widget_show(struct device *dev,
1298         struct device_attribute *attr, char *buf)
1299 {
1300         struct snd_soc_device *devdata = dev_get_drvdata(dev);
1301         struct snd_soc_codec *codec = devdata->card->codec;
1302         struct snd_soc_dapm_widget *w;
1303         int count = 0;
1304         char *state = "not set";
1305
1306         list_for_each_entry(w, &codec->dapm_widgets, list) {
1307
1308                 /* only display widgets that burnm power */
1309                 switch (w->id) {
1310                 case snd_soc_dapm_hp:
1311                 case snd_soc_dapm_mic:
1312                 case snd_soc_dapm_spk:
1313                 case snd_soc_dapm_line:
1314                 case snd_soc_dapm_micbias:
1315                 case snd_soc_dapm_dac:
1316                 case snd_soc_dapm_adc:
1317                 case snd_soc_dapm_pga:
1318                 case snd_soc_dapm_mixer:
1319                 case snd_soc_dapm_mixer_named_ctl:
1320                 case snd_soc_dapm_supply:
1321                         if (w->name)
1322                                 count += sprintf(buf + count, "%s: %s\n",
1323                                         w->name, w->power ? "On":"Off");
1324                 break;
1325                 default:
1326                 break;
1327                 }
1328         }
1329
1330         switch (codec->bias_level) {
1331         case SND_SOC_BIAS_ON:
1332                 state = "On";
1333                 break;
1334         case SND_SOC_BIAS_PREPARE:
1335                 state = "Prepare";
1336                 break;
1337         case SND_SOC_BIAS_STANDBY:
1338                 state = "Standby";
1339                 break;
1340         case SND_SOC_BIAS_OFF:
1341                 state = "Off";
1342                 break;
1343         }
1344         count += sprintf(buf + count, "PM State: %s\n", state);
1345
1346         return count;
1347 }
1348
1349 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1350
1351 int snd_soc_dapm_sys_add(struct device *dev)
1352 {
1353         return device_create_file(dev, &dev_attr_dapm_widget);
1354 }
1355
1356 static void snd_soc_dapm_sys_remove(struct device *dev)
1357 {
1358         device_remove_file(dev, &dev_attr_dapm_widget);
1359 }
1360
1361 /* free all dapm widgets and resources */
1362 static void dapm_free_widgets(struct snd_soc_codec *codec)
1363 {
1364         struct snd_soc_dapm_widget *w, *next_w;
1365         struct snd_soc_dapm_path *p, *next_p;
1366
1367         list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1368                 list_del(&w->list);
1369                 kfree(w);
1370         }
1371
1372         list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1373                 list_del(&p->list);
1374                 kfree(p->long_name);
1375                 kfree(p);
1376         }
1377 }
1378
1379 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1380                                 const char *pin, int status)
1381 {
1382         struct snd_soc_dapm_widget *w;
1383
1384         list_for_each_entry(w, &codec->dapm_widgets, list) {
1385                 if (!strcmp(w->name, pin)) {
1386                         pr_debug("dapm: %s: pin %s\n", codec->name, pin);
1387                         w->connected = status;
1388                         return 0;
1389                 }
1390         }
1391
1392         pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
1393         return -EINVAL;
1394 }
1395
1396 /**
1397  * snd_soc_dapm_sync - scan and power dapm paths
1398  * @codec: audio codec
1399  *
1400  * Walks all dapm audio paths and powers widgets according to their
1401  * stream or path usage.
1402  *
1403  * Returns 0 for success.
1404  */
1405 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
1406 {
1407         int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1408         dump_dapm(codec, "sync");
1409         return ret;
1410 }
1411 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1412
1413 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1414                                   const struct snd_soc_dapm_route *route)
1415 {
1416         struct snd_soc_dapm_path *path;
1417         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1418         const char *sink = route->sink;
1419         const char *control = route->control;
1420         const char *source = route->source;
1421         int ret = 0;
1422
1423         /* find src and dest widgets */
1424         list_for_each_entry(w, &codec->dapm_widgets, list) {
1425
1426                 if (!wsink && !(strcmp(w->name, sink))) {
1427                         wsink = w;
1428                         continue;
1429                 }
1430                 if (!wsource && !(strcmp(w->name, source))) {
1431                         wsource = w;
1432                 }
1433         }
1434
1435         if (wsource == NULL || wsink == NULL)
1436                 return -ENODEV;
1437
1438         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1439         if (!path)
1440                 return -ENOMEM;
1441
1442         path->source = wsource;
1443         path->sink = wsink;
1444         path->connected = route->connected;
1445         INIT_LIST_HEAD(&path->list);
1446         INIT_LIST_HEAD(&path->list_source);
1447         INIT_LIST_HEAD(&path->list_sink);
1448
1449         /* check for external widgets */
1450         if (wsink->id == snd_soc_dapm_input) {
1451                 if (wsource->id == snd_soc_dapm_micbias ||
1452                         wsource->id == snd_soc_dapm_mic ||
1453                         wsource->id == snd_soc_dapm_line ||
1454                         wsource->id == snd_soc_dapm_output)
1455                         wsink->ext = 1;
1456         }
1457         if (wsource->id == snd_soc_dapm_output) {
1458                 if (wsink->id == snd_soc_dapm_spk ||
1459                         wsink->id == snd_soc_dapm_hp ||
1460                         wsink->id == snd_soc_dapm_line ||
1461                         wsink->id == snd_soc_dapm_input)
1462                         wsource->ext = 1;
1463         }
1464
1465         /* connect static paths */
1466         if (control == NULL) {
1467                 list_add(&path->list, &codec->dapm_paths);
1468                 list_add(&path->list_sink, &wsink->sources);
1469                 list_add(&path->list_source, &wsource->sinks);
1470                 path->connect = 1;
1471                 return 0;
1472         }
1473
1474         /* connect dynamic paths */
1475         switch(wsink->id) {
1476         case snd_soc_dapm_adc:
1477         case snd_soc_dapm_dac:
1478         case snd_soc_dapm_pga:
1479         case snd_soc_dapm_input:
1480         case snd_soc_dapm_output:
1481         case snd_soc_dapm_micbias:
1482         case snd_soc_dapm_vmid:
1483         case snd_soc_dapm_pre:
1484         case snd_soc_dapm_post:
1485         case snd_soc_dapm_supply:
1486         case snd_soc_dapm_aif_in:
1487         case snd_soc_dapm_aif_out:
1488                 list_add(&path->list, &codec->dapm_paths);
1489                 list_add(&path->list_sink, &wsink->sources);
1490                 list_add(&path->list_source, &wsource->sinks);
1491                 path->connect = 1;
1492                 return 0;
1493         case snd_soc_dapm_mux:
1494         case snd_soc_dapm_value_mux:
1495                 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1496                         &wsink->kcontrols[0]);
1497                 if (ret != 0)
1498                         goto err;
1499                 break;
1500         case snd_soc_dapm_switch:
1501         case snd_soc_dapm_mixer:
1502         case snd_soc_dapm_mixer_named_ctl:
1503                 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1504                 if (ret != 0)
1505                         goto err;
1506                 break;
1507         case snd_soc_dapm_hp:
1508         case snd_soc_dapm_mic:
1509         case snd_soc_dapm_line:
1510         case snd_soc_dapm_spk:
1511                 list_add(&path->list, &codec->dapm_paths);
1512                 list_add(&path->list_sink, &wsink->sources);
1513                 list_add(&path->list_source, &wsource->sinks);
1514                 path->connect = 0;
1515                 return 0;
1516         }
1517         return 0;
1518
1519 err:
1520         printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1521                 control, sink);
1522         kfree(path);
1523         return ret;
1524 }
1525
1526 /**
1527  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1528  * @codec: codec
1529  * @route: audio routes
1530  * @num: number of routes
1531  *
1532  * Connects 2 dapm widgets together via a named audio path. The sink is
1533  * the widget receiving the audio signal, whilst the source is the sender
1534  * of the audio signal.
1535  *
1536  * Returns 0 for success else error. On error all resources can be freed
1537  * with a call to snd_soc_card_free().
1538  */
1539 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1540                             const struct snd_soc_dapm_route *route, int num)
1541 {
1542         int i, ret;
1543
1544         for (i = 0; i < num; i++) {
1545                 ret = snd_soc_dapm_add_route(codec, route);
1546                 if (ret < 0) {
1547                         printk(KERN_ERR "Failed to add route %s->%s\n",
1548                                route->source,
1549                                route->sink);
1550                         return ret;
1551                 }
1552                 route++;
1553         }
1554
1555         return 0;
1556 }
1557 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1558
1559 /**
1560  * snd_soc_dapm_new_widgets - add new dapm widgets
1561  * @codec: audio codec
1562  *
1563  * Checks the codec for any new dapm widgets and creates them if found.
1564  *
1565  * Returns 0 for success.
1566  */
1567 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1568 {
1569         struct snd_soc_dapm_widget *w;
1570
1571         list_for_each_entry(w, &codec->dapm_widgets, list)
1572         {
1573                 if (w->new)
1574                         continue;
1575
1576                 switch(w->id) {
1577                 case snd_soc_dapm_switch:
1578                 case snd_soc_dapm_mixer:
1579                 case snd_soc_dapm_mixer_named_ctl:
1580                         w->power_check = dapm_generic_check_power;
1581                         dapm_new_mixer(codec, w);
1582                         break;
1583                 case snd_soc_dapm_mux:
1584                 case snd_soc_dapm_value_mux:
1585                         w->power_check = dapm_generic_check_power;
1586                         dapm_new_mux(codec, w);
1587                         break;
1588                 case snd_soc_dapm_adc:
1589                 case snd_soc_dapm_aif_out:
1590                         w->power_check = dapm_adc_check_power;
1591                         break;
1592                 case snd_soc_dapm_dac:
1593                 case snd_soc_dapm_aif_in:
1594                         w->power_check = dapm_dac_check_power;
1595                         break;
1596                 case snd_soc_dapm_pga:
1597                         w->power_check = dapm_generic_check_power;
1598                         dapm_new_pga(codec, w);
1599                         break;
1600                 case snd_soc_dapm_input:
1601                 case snd_soc_dapm_output:
1602                 case snd_soc_dapm_micbias:
1603                 case snd_soc_dapm_spk:
1604                 case snd_soc_dapm_hp:
1605                 case snd_soc_dapm_mic:
1606                 case snd_soc_dapm_line:
1607                         w->power_check = dapm_generic_check_power;
1608                         break;
1609                 case snd_soc_dapm_supply:
1610                         w->power_check = dapm_supply_check_power;
1611                 case snd_soc_dapm_vmid:
1612                 case snd_soc_dapm_pre:
1613                 case snd_soc_dapm_post:
1614                         break;
1615                 }
1616                 w->new = 1;
1617         }
1618
1619         dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1620         return 0;
1621 }
1622 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1623
1624 /**
1625  * snd_soc_dapm_get_volsw - dapm mixer get callback
1626  * @kcontrol: mixer control
1627  * @ucontrol: control element information
1628  *
1629  * Callback to get the value of a dapm mixer control.
1630  *
1631  * Returns 0 for success.
1632  */
1633 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1634         struct snd_ctl_elem_value *ucontrol)
1635 {
1636         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1637         struct soc_mixer_control *mc =
1638                 (struct soc_mixer_control *)kcontrol->private_value;
1639         unsigned int reg = mc->reg;
1640         unsigned int shift = mc->shift;
1641         unsigned int rshift = mc->rshift;
1642         int max = mc->max;
1643         unsigned int invert = mc->invert;
1644         unsigned int mask = (1 << fls(max)) - 1;
1645
1646         /* return the saved value if we are powered down */
1647         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1648                 ucontrol->value.integer.value[0] = widget->saved_value;
1649                 return 0;
1650         }
1651
1652         ucontrol->value.integer.value[0] =
1653                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1654         if (shift != rshift)
1655                 ucontrol->value.integer.value[1] =
1656                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1657         if (invert) {
1658                 ucontrol->value.integer.value[0] =
1659                         max - ucontrol->value.integer.value[0];
1660                 if (shift != rshift)
1661                         ucontrol->value.integer.value[1] =
1662                                 max - ucontrol->value.integer.value[1];
1663         }
1664
1665         return 0;
1666 }
1667 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1668
1669 /**
1670  * snd_soc_dapm_put_volsw - dapm mixer set callback
1671  * @kcontrol: mixer control
1672  * @ucontrol: control element information
1673  *
1674  * Callback to set the value of a dapm mixer control.
1675  *
1676  * Returns 0 for success.
1677  */
1678 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1679         struct snd_ctl_elem_value *ucontrol)
1680 {
1681         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1682         struct soc_mixer_control *mc =
1683                 (struct soc_mixer_control *)kcontrol->private_value;
1684         unsigned int reg = mc->reg;
1685         unsigned int shift = mc->shift;
1686         unsigned int rshift = mc->rshift;
1687         int max = mc->max;
1688         unsigned int mask = (1 << fls(max)) - 1;
1689         unsigned int invert = mc->invert;
1690         unsigned int val, val2, val_mask;
1691         int connect;
1692         int ret;
1693
1694         val = (ucontrol->value.integer.value[0] & mask);
1695
1696         if (invert)
1697                 val = max - val;
1698         val_mask = mask << shift;
1699         val = val << shift;
1700         if (shift != rshift) {
1701                 val2 = (ucontrol->value.integer.value[1] & mask);
1702                 if (invert)
1703                         val2 = max - val2;
1704                 val_mask |= mask << rshift;
1705                 val |= val2 << rshift;
1706         }
1707
1708         mutex_lock(&widget->codec->mutex);
1709         widget->value = val;
1710
1711         /* save volume value if the widget is powered down */
1712         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1713                 widget->saved_value = val;
1714                 mutex_unlock(&widget->codec->mutex);
1715                 return 1;
1716         }
1717
1718         if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1719                 if (val)
1720                         /* new connection */
1721                         connect = invert ? 0:1;
1722                 else
1723                         /* old connection must be powered down */
1724                         connect = invert ? 1:0;
1725
1726                 dapm_mixer_update_power(widget, kcontrol, connect);
1727         }
1728
1729         if (widget->event) {
1730                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1731                         ret = widget->event(widget, kcontrol,
1732                                                 SND_SOC_DAPM_PRE_REG);
1733                         if (ret < 0) {
1734                                 ret = 1;
1735                                 goto out;
1736                         }
1737                 }
1738                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1739                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1740                         ret = widget->event(widget, kcontrol,
1741                                                 SND_SOC_DAPM_POST_REG);
1742         } else
1743                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1744
1745 out:
1746         mutex_unlock(&widget->codec->mutex);
1747         return ret;
1748 }
1749 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1750
1751 /**
1752  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1753  * @kcontrol: mixer control
1754  * @ucontrol: control element information
1755  *
1756  * Callback to get the value of a dapm enumerated double mixer control.
1757  *
1758  * Returns 0 for success.
1759  */
1760 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1761         struct snd_ctl_elem_value *ucontrol)
1762 {
1763         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1764         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1765         unsigned int val, bitmask;
1766
1767         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1768                 ;
1769         val = snd_soc_read(widget->codec, e->reg);
1770         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1771         if (e->shift_l != e->shift_r)
1772                 ucontrol->value.enumerated.item[1] =
1773                         (val >> e->shift_r) & (bitmask - 1);
1774
1775         return 0;
1776 }
1777 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1778
1779 /**
1780  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1781  * @kcontrol: mixer control
1782  * @ucontrol: control element information
1783  *
1784  * Callback to set the value of a dapm enumerated double mixer control.
1785  *
1786  * Returns 0 for success.
1787  */
1788 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1789         struct snd_ctl_elem_value *ucontrol)
1790 {
1791         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1792         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1793         unsigned int val, mux, change;
1794         unsigned int mask, bitmask;
1795         int ret = 0;
1796
1797         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1798                 ;
1799         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1800                 return -EINVAL;
1801         mux = ucontrol->value.enumerated.item[0];
1802         val = mux << e->shift_l;
1803         mask = (bitmask - 1) << e->shift_l;
1804         if (e->shift_l != e->shift_r) {
1805                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1806                         return -EINVAL;
1807                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1808                 mask |= (bitmask - 1) << e->shift_r;
1809         }
1810
1811         mutex_lock(&widget->codec->mutex);
1812         widget->value = val;
1813         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1814         dapm_mux_update_power(widget, kcontrol, change, mux, e);
1815
1816         if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1817                 ret = widget->event(widget,
1818                                     kcontrol, SND_SOC_DAPM_PRE_REG);
1819                 if (ret < 0)
1820                         goto out;
1821         }
1822
1823         ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1824
1825         if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1826                 ret = widget->event(widget,
1827                                     kcontrol, SND_SOC_DAPM_POST_REG);
1828
1829 out:
1830         mutex_unlock(&widget->codec->mutex);
1831         return ret;
1832 }
1833 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1834
1835 /**
1836  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1837  * @kcontrol: mixer control
1838  * @ucontrol: control element information
1839  *
1840  * Returns 0 for success.
1841  */
1842 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1843                                struct snd_ctl_elem_value *ucontrol)
1844 {
1845         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1846
1847         ucontrol->value.enumerated.item[0] = widget->value;
1848
1849         return 0;
1850 }
1851 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1852
1853 /**
1854  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1855  * @kcontrol: mixer control
1856  * @ucontrol: control element information
1857  *
1858  * Returns 0 for success.
1859  */
1860 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1861                                struct snd_ctl_elem_value *ucontrol)
1862 {
1863         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1864         struct soc_enum *e =
1865                 (struct soc_enum *)kcontrol->private_value;
1866         int change;
1867         int ret = 0;
1868
1869         if (ucontrol->value.enumerated.item[0] >= e->max)
1870                 return -EINVAL;
1871
1872         mutex_lock(&widget->codec->mutex);
1873
1874         change = widget->value != ucontrol->value.enumerated.item[0];
1875         widget->value = ucontrol->value.enumerated.item[0];
1876         dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1877
1878         mutex_unlock(&widget->codec->mutex);
1879         return ret;
1880 }
1881 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1882
1883 /**
1884  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1885  *                                      callback
1886  * @kcontrol: mixer control
1887  * @ucontrol: control element information
1888  *
1889  * Callback to get the value of a dapm semi enumerated double mixer control.
1890  *
1891  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1892  * used for handling bitfield coded enumeration for example.
1893  *
1894  * Returns 0 for success.
1895  */
1896 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1897         struct snd_ctl_elem_value *ucontrol)
1898 {
1899         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1900         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1901         unsigned int reg_val, val, mux;
1902
1903         reg_val = snd_soc_read(widget->codec, e->reg);
1904         val = (reg_val >> e->shift_l) & e->mask;
1905         for (mux = 0; mux < e->max; mux++) {
1906                 if (val == e->values[mux])
1907                         break;
1908         }
1909         ucontrol->value.enumerated.item[0] = mux;
1910         if (e->shift_l != e->shift_r) {
1911                 val = (reg_val >> e->shift_r) & e->mask;
1912                 for (mux = 0; mux < e->max; mux++) {
1913                         if (val == e->values[mux])
1914                                 break;
1915                 }
1916                 ucontrol->value.enumerated.item[1] = mux;
1917         }
1918
1919         return 0;
1920 }
1921 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1922
1923 /**
1924  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1925  *                                      callback
1926  * @kcontrol: mixer control
1927  * @ucontrol: control element information
1928  *
1929  * Callback to set the value of a dapm semi enumerated double mixer control.
1930  *
1931  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1932  * used for handling bitfield coded enumeration for example.
1933  *
1934  * Returns 0 for success.
1935  */
1936 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1937         struct snd_ctl_elem_value *ucontrol)
1938 {
1939         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1940         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1941         unsigned int val, mux, change;
1942         unsigned int mask;
1943         int ret = 0;
1944
1945         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1946                 return -EINVAL;
1947         mux = ucontrol->value.enumerated.item[0];
1948         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1949         mask = e->mask << e->shift_l;
1950         if (e->shift_l != e->shift_r) {
1951                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1952                         return -EINVAL;
1953                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1954                 mask |= e->mask << e->shift_r;
1955         }
1956
1957         mutex_lock(&widget->codec->mutex);
1958         widget->value = val;
1959         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1960         dapm_mux_update_power(widget, kcontrol, change, mux, e);
1961
1962         if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1963                 ret = widget->event(widget,
1964                                     kcontrol, SND_SOC_DAPM_PRE_REG);
1965                 if (ret < 0)
1966                         goto out;
1967         }
1968
1969         ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1970
1971         if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1972                 ret = widget->event(widget,
1973                                     kcontrol, SND_SOC_DAPM_POST_REG);
1974
1975 out:
1976         mutex_unlock(&widget->codec->mutex);
1977         return ret;
1978 }
1979 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1980
1981 /**
1982  * snd_soc_dapm_info_pin_switch - Info for a pin switch
1983  *
1984  * @kcontrol: mixer control
1985  * @uinfo: control element information
1986  *
1987  * Callback to provide information about a pin switch control.
1988  */
1989 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1990                                  struct snd_ctl_elem_info *uinfo)
1991 {
1992         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1993         uinfo->count = 1;
1994         uinfo->value.integer.min = 0;
1995         uinfo->value.integer.max = 1;
1996
1997         return 0;
1998 }
1999 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2000
2001 /**
2002  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2003  *
2004  * @kcontrol: mixer control
2005  * @ucontrol: Value
2006  */
2007 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2008                                 struct snd_ctl_elem_value *ucontrol)
2009 {
2010         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2011         const char *pin = (const char *)kcontrol->private_value;
2012
2013         mutex_lock(&codec->mutex);
2014
2015         ucontrol->value.integer.value[0] =
2016                 snd_soc_dapm_get_pin_status(codec, pin);
2017
2018         mutex_unlock(&codec->mutex);
2019
2020         return 0;
2021 }
2022 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2023
2024 /**
2025  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2026  *
2027  * @kcontrol: mixer control
2028  * @ucontrol: Value
2029  */
2030 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2031                                 struct snd_ctl_elem_value *ucontrol)
2032 {
2033         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2034         const char *pin = (const char *)kcontrol->private_value;
2035
2036         mutex_lock(&codec->mutex);
2037
2038         if (ucontrol->value.integer.value[0])
2039                 snd_soc_dapm_enable_pin(codec, pin);
2040         else
2041                 snd_soc_dapm_disable_pin(codec, pin);
2042
2043         snd_soc_dapm_sync(codec);
2044
2045         mutex_unlock(&codec->mutex);
2046
2047         return 0;
2048 }
2049 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2050
2051 /**
2052  * snd_soc_dapm_new_control - create new dapm control
2053  * @codec: audio codec
2054  * @widget: widget template
2055  *
2056  * Creates a new dapm control based upon the template.
2057  *
2058  * Returns 0 for success else error.
2059  */
2060 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
2061         const struct snd_soc_dapm_widget *widget)
2062 {
2063         struct snd_soc_dapm_widget *w;
2064
2065         if ((w = dapm_cnew_widget(widget)) == NULL)
2066                 return -ENOMEM;
2067
2068         w->codec = codec;
2069         INIT_LIST_HEAD(&w->sources);
2070         INIT_LIST_HEAD(&w->sinks);
2071         INIT_LIST_HEAD(&w->list);
2072         list_add(&w->list, &codec->dapm_widgets);
2073
2074         /* machine layer set ups unconnected pins and insertions */
2075         w->connected = 1;
2076         return 0;
2077 }
2078 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2079
2080 /**
2081  * snd_soc_dapm_new_controls - create new dapm controls
2082  * @codec: audio codec
2083  * @widget: widget array
2084  * @num: number of widgets
2085  *
2086  * Creates new DAPM controls based upon the templates.
2087  *
2088  * Returns 0 for success else error.
2089  */
2090 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
2091         const struct snd_soc_dapm_widget *widget,
2092         int num)
2093 {
2094         int i, ret;
2095
2096         for (i = 0; i < num; i++) {
2097                 ret = snd_soc_dapm_new_control(codec, widget);
2098                 if (ret < 0) {
2099                         printk(KERN_ERR
2100                                "ASoC: Failed to create DAPM control %s: %d\n",
2101                                widget->name, ret);
2102                         return ret;
2103                 }
2104                 widget++;
2105         }
2106         return 0;
2107 }
2108 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2109
2110
2111 /**
2112  * snd_soc_dapm_stream_event - send a stream event to the dapm core
2113  * @codec: audio codec
2114  * @stream: stream name
2115  * @event: stream event
2116  *
2117  * Sends a stream event to the dapm core. The core then makes any
2118  * necessary widget power changes.
2119  *
2120  * Returns 0 for success else error.
2121  */
2122 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2123         char *stream, int event)
2124 {
2125         struct snd_soc_dapm_widget *w;
2126
2127         if (stream == NULL)
2128                 return 0;
2129
2130         mutex_lock(&codec->mutex);
2131         list_for_each_entry(w, &codec->dapm_widgets, list)
2132         {
2133                 if (!w->sname)
2134                         continue;
2135                 pr_debug("widget %s\n %s stream %s event %d\n",
2136                          w->name, w->sname, stream, event);
2137                 if (strstr(w->sname, stream)) {
2138                         switch(event) {
2139                         case SND_SOC_DAPM_STREAM_START:
2140                                 w->active = 1;
2141                                 break;
2142                         case SND_SOC_DAPM_STREAM_STOP:
2143                                 w->active = 0;
2144                                 break;
2145                         case SND_SOC_DAPM_STREAM_SUSPEND:
2146                                 if (w->active)
2147                                         w->suspend = 1;
2148                                 w->active = 0;
2149                                 break;
2150                         case SND_SOC_DAPM_STREAM_RESUME:
2151                                 if (w->suspend) {
2152                                         w->active = 1;
2153                                         w->suspend = 0;
2154                                 }
2155                                 break;
2156                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2157                                 break;
2158                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2159                                 break;
2160                         }
2161                 }
2162         }
2163
2164         dapm_power_widgets(codec, event);
2165         mutex_unlock(&codec->mutex);
2166         dump_dapm(codec, __func__);
2167         return 0;
2168 }
2169 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2170
2171 /**
2172  * snd_soc_dapm_enable_pin - enable pin.
2173  * @codec: SoC codec
2174  * @pin: pin name
2175  *
2176  * Enables input/output pin and its parents or children widgets iff there is
2177  * a valid audio route and active audio stream.
2178  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2179  * do any widget power switching.
2180  */
2181 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
2182 {
2183         return snd_soc_dapm_set_pin(codec, pin, 1);
2184 }
2185 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2186
2187 /**
2188  * snd_soc_dapm_disable_pin - disable pin.
2189  * @codec: SoC codec
2190  * @pin: pin name
2191  *
2192  * Disables input/output pin and its parents or children widgets.
2193  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2194  * do any widget power switching.
2195  */
2196 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
2197 {
2198         return snd_soc_dapm_set_pin(codec, pin, 0);
2199 }
2200 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2201
2202 /**
2203  * snd_soc_dapm_nc_pin - permanently disable pin.
2204  * @codec: SoC codec
2205  * @pin: pin name
2206  *
2207  * Marks the specified pin as being not connected, disabling it along
2208  * any parent or child widgets.  At present this is identical to
2209  * snd_soc_dapm_disable_pin() but in future it will be extended to do
2210  * additional things such as disabling controls which only affect
2211  * paths through the pin.
2212  *
2213  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2214  * do any widget power switching.
2215  */
2216 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
2217 {
2218         return snd_soc_dapm_set_pin(codec, pin, 0);
2219 }
2220 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2221
2222 /**
2223  * snd_soc_dapm_get_pin_status - get audio pin status
2224  * @codec: audio codec
2225  * @pin: audio signal pin endpoint (or start point)
2226  *
2227  * Get audio pin status - connected or disconnected.
2228  *
2229  * Returns 1 for connected otherwise 0.
2230  */
2231 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
2232 {
2233         struct snd_soc_dapm_widget *w;
2234
2235         list_for_each_entry(w, &codec->dapm_widgets, list) {
2236                 if (!strcmp(w->name, pin))
2237                         return w->connected;
2238         }
2239
2240         return 0;
2241 }
2242 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2243
2244 /**
2245  * snd_soc_dapm_free - free dapm resources
2246  * @socdev: SoC device
2247  *
2248  * Free all dapm widgets and resources.
2249  */
2250 void snd_soc_dapm_free(struct snd_soc_device *socdev)
2251 {
2252         struct snd_soc_codec *codec = socdev->card->codec;
2253
2254         snd_soc_dapm_sys_remove(socdev->dev);
2255         dapm_free_widgets(codec);
2256 }
2257 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2258
2259 /*
2260  * snd_soc_dapm_shutdown - callback for system shutdown
2261  */
2262 void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2263 {
2264         struct snd_soc_codec *codec = socdev->card->codec;
2265         struct snd_soc_dapm_widget *w;
2266         LIST_HEAD(down_list);
2267         int powerdown = 0;
2268
2269         list_for_each_entry(w, &codec->dapm_widgets, list) {
2270                 if (w->power) {
2271                         dapm_seq_insert(w, &down_list, dapm_down_seq);
2272                         w->power = 0;
2273                         powerdown = 1;
2274                 }
2275         }
2276
2277         /* If there were no widgets to power down we're already in
2278          * standby.
2279          */
2280         if (powerdown) {
2281                 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2282                 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2283                 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2284         }
2285
2286         snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2287 }
2288
2289 /* Module information */
2290 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2291 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2292 MODULE_LICENSE("GPL");