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