sysdev: Convert cpu driver sysdev class attributes
[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                 pop_wait(pop_time);
101         }
102
103         va_end(args);
104 }
105
106 /* create a new dapm widget */
107 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
108         const struct snd_soc_dapm_widget *_widget)
109 {
110         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
111 }
112
113 /**
114  * snd_soc_dapm_set_bias_level - set the bias level for the system
115  * @socdev: audio device
116  * @level: level to configure
117  *
118  * Configure the bias (power) levels for the SoC audio device.
119  *
120  * Returns 0 for success else error.
121  */
122 static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
123                                        enum snd_soc_bias_level level)
124 {
125         struct snd_soc_card *card = socdev->card;
126         struct snd_soc_codec *codec = socdev->card->codec;
127         int ret = 0;
128
129         switch (level) {
130         case SND_SOC_BIAS_ON:
131                 dev_dbg(socdev->dev, "Setting full bias\n");
132                 break;
133         case SND_SOC_BIAS_PREPARE:
134                 dev_dbg(socdev->dev, "Setting bias prepare\n");
135                 break;
136         case SND_SOC_BIAS_STANDBY:
137                 dev_dbg(socdev->dev, "Setting standby bias\n");
138                 break;
139         case SND_SOC_BIAS_OFF:
140                 dev_dbg(socdev->dev, "Setting bias off\n");
141                 break;
142         default:
143                 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
144                 return -EINVAL;
145         }
146
147         if (card->set_bias_level)
148                 ret = card->set_bias_level(card, level);
149         if (ret == 0) {
150                 if (codec->set_bias_level)
151                         ret = codec->set_bias_level(codec, level);
152                 else
153                         codec->bias_level = level;
154         }
155
156         return ret;
157 }
158
159 /* set up initial codec paths */
160 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
161         struct snd_soc_dapm_path *p, int i)
162 {
163         switch (w->id) {
164         case snd_soc_dapm_switch:
165         case snd_soc_dapm_mixer:
166         case snd_soc_dapm_mixer_named_ctl: {
167                 int val;
168                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
169                         w->kcontrols[i].private_value;
170                 unsigned int reg = mc->reg;
171                 unsigned int shift = mc->shift;
172                 int max = mc->max;
173                 unsigned int mask = (1 << fls(max)) - 1;
174                 unsigned int invert = mc->invert;
175
176                 val = snd_soc_read(w->codec, reg);
177                 val = (val >> shift) & mask;
178
179                 if ((invert && !val) || (!invert && val))
180                         p->connect = 1;
181                 else
182                         p->connect = 0;
183         }
184         break;
185         case snd_soc_dapm_mux: {
186                 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
187                 int val, item, bitmask;
188
189                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
190                 ;
191                 val = snd_soc_read(w->codec, e->reg);
192                 item = (val >> e->shift_l) & (bitmask - 1);
193
194                 p->connect = 0;
195                 for (i = 0; i < e->max; i++) {
196                         if (!(strcmp(p->name, e->texts[i])) && item == i)
197                                 p->connect = 1;
198                 }
199         }
200         break;
201         case snd_soc_dapm_value_mux: {
202                 struct soc_enum *e = (struct soc_enum *)
203                         w->kcontrols[i].private_value;
204                 int val, item;
205
206                 val = snd_soc_read(w->codec, e->reg);
207                 val = (val >> e->shift_l) & e->mask;
208                 for (item = 0; item < e->max; item++) {
209                         if (val == e->values[item])
210                                 break;
211                 }
212
213                 p->connect = 0;
214                 for (i = 0; i < e->max; i++) {
215                         if (!(strcmp(p->name, e->texts[i])) && item == i)
216                                 p->connect = 1;
217                 }
218         }
219         break;
220         /* does not effect routing - always connected */
221         case snd_soc_dapm_pga:
222         case snd_soc_dapm_output:
223         case snd_soc_dapm_adc:
224         case snd_soc_dapm_input:
225         case snd_soc_dapm_dac:
226         case snd_soc_dapm_micbias:
227         case snd_soc_dapm_vmid:
228         case snd_soc_dapm_supply:
229         case snd_soc_dapm_aif_in:
230         case snd_soc_dapm_aif_out:
231                 p->connect = 1;
232         break;
233         /* does effect routing - dynamically connected */
234         case snd_soc_dapm_hp:
235         case snd_soc_dapm_mic:
236         case snd_soc_dapm_spk:
237         case snd_soc_dapm_line:
238         case snd_soc_dapm_pre:
239         case snd_soc_dapm_post:
240                 p->connect = 0;
241         break;
242         }
243 }
244
245 /* connect mux widget to its interconnecting audio paths */
246 static int dapm_connect_mux(struct snd_soc_codec *codec,
247         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
248         struct snd_soc_dapm_path *path, const char *control_name,
249         const struct snd_kcontrol_new *kcontrol)
250 {
251         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
252         int i;
253
254         for (i = 0; i < e->max; i++) {
255                 if (!(strcmp(control_name, e->texts[i]))) {
256                         list_add(&path->list, &codec->dapm_paths);
257                         list_add(&path->list_sink, &dest->sources);
258                         list_add(&path->list_source, &src->sinks);
259                         path->name = (char*)e->texts[i];
260                         dapm_set_path_status(dest, path, 0);
261                         return 0;
262                 }
263         }
264
265         return -ENODEV;
266 }
267
268 /* connect mixer widget to its interconnecting audio paths */
269 static int dapm_connect_mixer(struct snd_soc_codec *codec,
270         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
271         struct snd_soc_dapm_path *path, const char *control_name)
272 {
273         int i;
274
275         /* search for mixer kcontrol */
276         for (i = 0; i < dest->num_kcontrols; i++) {
277                 if (!strcmp(control_name, dest->kcontrols[i].name)) {
278                         list_add(&path->list, &codec->dapm_paths);
279                         list_add(&path->list_sink, &dest->sources);
280                         list_add(&path->list_source, &src->sinks);
281                         path->name = dest->kcontrols[i].name;
282                         dapm_set_path_status(dest, path, i);
283                         return 0;
284                 }
285         }
286         return -ENODEV;
287 }
288
289 /* update dapm codec register bits */
290 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
291 {
292         int change, power;
293         unsigned int old, new;
294         struct snd_soc_codec *codec = widget->codec;
295
296         /* check for valid widgets */
297         if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
298                 widget->id == snd_soc_dapm_output ||
299                 widget->id == snd_soc_dapm_hp ||
300                 widget->id == snd_soc_dapm_mic ||
301                 widget->id == snd_soc_dapm_line ||
302                 widget->id == snd_soc_dapm_spk)
303                 return 0;
304
305         power = widget->power;
306         if (widget->invert)
307                 power = (power ? 0:1);
308
309         old = snd_soc_read(codec, widget->reg);
310         new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
311
312         change = old != new;
313         if (change) {
314                 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
315                         widget->name, widget->power ? "on" : "off",
316                         codec->pop_time);
317                 snd_soc_write(codec, widget->reg, new);
318                 pop_wait(codec->pop_time);
319         }
320         pr_debug("reg %x old %x new %x change %d\n", widget->reg,
321                  old, new, change);
322         return change;
323 }
324
325 /* ramps the volume up or down to minimise pops before or after a
326  * DAPM power event */
327 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
328 {
329         const struct snd_kcontrol_new *k = widget->kcontrols;
330
331         if (widget->muted && !power)
332                 return 0;
333         if (!widget->muted && power)
334                 return 0;
335
336         if (widget->num_kcontrols && k) {
337                 struct soc_mixer_control *mc =
338                         (struct soc_mixer_control *)k->private_value;
339                 unsigned int reg = mc->reg;
340                 unsigned int shift = mc->shift;
341                 int max = mc->max;
342                 unsigned int mask = (1 << fls(max)) - 1;
343                 unsigned int invert = mc->invert;
344
345                 if (power) {
346                         int i;
347                         /* power up has happended, increase volume to last level */
348                         if (invert) {
349                                 for (i = max; i > widget->saved_value; i--)
350                                         snd_soc_update_bits(widget->codec, reg, mask, i);
351                         } else {
352                                 for (i = 0; i < widget->saved_value; i++)
353                                         snd_soc_update_bits(widget->codec, reg, mask, i);
354                         }
355                         widget->muted = 0;
356                 } else {
357                         /* power down is about to occur, decrease volume to mute */
358                         int val = snd_soc_read(widget->codec, reg);
359                         int i = widget->saved_value = (val >> shift) & mask;
360                         if (invert) {
361                                 for (; i < mask; i++)
362                                         snd_soc_update_bits(widget->codec, reg, mask, i);
363                         } else {
364                                 for (; i > 0; i--)
365                                         snd_soc_update_bits(widget->codec, reg, mask, i);
366                         }
367                         widget->muted = 1;
368                 }
369         }
370         return 0;
371 }
372
373 /* create new dapm mixer control */
374 static int dapm_new_mixer(struct snd_soc_codec *codec,
375         struct snd_soc_dapm_widget *w)
376 {
377         int i, ret = 0;
378         size_t name_len;
379         struct snd_soc_dapm_path *path;
380
381         /* add kcontrol */
382         for (i = 0; i < w->num_kcontrols; i++) {
383
384                 /* match name */
385                 list_for_each_entry(path, &w->sources, list_sink) {
386
387                         /* mixer/mux paths name must match control name */
388                         if (path->name != (char*)w->kcontrols[i].name)
389                                 continue;
390
391                         /* add dapm control with long name.
392                          * for dapm_mixer this is the concatenation of the
393                          * mixer and kcontrol name.
394                          * for dapm_mixer_named_ctl this is simply the
395                          * kcontrol name.
396                          */
397                         name_len = strlen(w->kcontrols[i].name) + 1;
398                         if (w->id != snd_soc_dapm_mixer_named_ctl)
399                                 name_len += 1 + strlen(w->name);
400
401                         path->long_name = kmalloc(name_len, GFP_KERNEL);
402
403                         if (path->long_name == NULL)
404                                 return -ENOMEM;
405
406                         switch (w->id) {
407                         default:
408                                 snprintf(path->long_name, name_len, "%s %s",
409                                          w->name, w->kcontrols[i].name);
410                                 break;
411                         case snd_soc_dapm_mixer_named_ctl:
412                                 snprintf(path->long_name, name_len, "%s",
413                                          w->kcontrols[i].name);
414                                 break;
415                         }
416
417                         path->long_name[name_len - 1] = '\0';
418
419                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
420                                 path->long_name);
421                         ret = snd_ctl_add(codec->card, path->kcontrol);
422                         if (ret < 0) {
423                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
424                                        path->long_name,
425                                        ret);
426                                 kfree(path->long_name);
427                                 path->long_name = NULL;
428                                 return ret;
429                         }
430                 }
431         }
432         return ret;
433 }
434
435 /* create new dapm mux control */
436 static int dapm_new_mux(struct snd_soc_codec *codec,
437         struct snd_soc_dapm_widget *w)
438 {
439         struct snd_soc_dapm_path *path = NULL;
440         struct snd_kcontrol *kcontrol;
441         int ret = 0;
442
443         if (!w->num_kcontrols) {
444                 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
445                 return -EINVAL;
446         }
447
448         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
449         ret = snd_ctl_add(codec->card, kcontrol);
450         if (ret < 0)
451                 goto err;
452
453         list_for_each_entry(path, &w->sources, list_sink)
454                 path->kcontrol = kcontrol;
455
456         return ret;
457
458 err:
459         printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
460         return ret;
461 }
462
463 /* create new dapm volume control */
464 static int dapm_new_pga(struct snd_soc_codec *codec,
465         struct snd_soc_dapm_widget *w)
466 {
467         struct snd_kcontrol *kcontrol;
468         int ret = 0;
469
470         if (!w->num_kcontrols)
471                 return -EINVAL;
472
473         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
474         ret = snd_ctl_add(codec->card, kcontrol);
475         if (ret < 0) {
476                 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
477                 return ret;
478         }
479
480         return ret;
481 }
482
483 /* reset 'walked' bit for each dapm path */
484 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
485 {
486         struct snd_soc_dapm_path *p;
487
488         list_for_each_entry(p, &codec->dapm_paths, list)
489                 p->walked = 0;
490 }
491
492 /*
493  * Recursively check for a completed path to an active or physically connected
494  * output widget. Returns number of complete paths.
495  */
496 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
497 {
498         struct snd_soc_dapm_path *path;
499         int con = 0;
500
501         if (widget->id == snd_soc_dapm_supply)
502                 return 0;
503
504         switch (widget->id) {
505         case snd_soc_dapm_adc:
506         case snd_soc_dapm_aif_out:
507                 if (widget->active)
508                         return 1;
509         default:
510                 break;
511         }
512
513         if (widget->connected) {
514                 /* connected pin ? */
515                 if (widget->id == snd_soc_dapm_output && !widget->ext)
516                         return 1;
517
518                 /* connected jack or spk ? */
519                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
520                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
521                         return 1;
522         }
523
524         list_for_each_entry(path, &widget->sinks, list_source) {
525                 if (path->walked)
526                         continue;
527
528                 if (path->sink && path->connect) {
529                         path->walked = 1;
530                         con += is_connected_output_ep(path->sink);
531                 }
532         }
533
534         return con;
535 }
536
537 /*
538  * Recursively check for a completed path to an active or physically connected
539  * input widget. Returns number of complete paths.
540  */
541 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
542 {
543         struct snd_soc_dapm_path *path;
544         int con = 0;
545
546         if (widget->id == snd_soc_dapm_supply)
547                 return 0;
548
549         /* active stream ? */
550         switch (widget->id) {
551         case snd_soc_dapm_dac:
552         case snd_soc_dapm_aif_in:
553                 if (widget->active)
554                         return 1;
555         default:
556                 break;
557         }
558
559         if (widget->connected) {
560                 /* connected pin ? */
561                 if (widget->id == snd_soc_dapm_input && !widget->ext)
562                         return 1;
563
564                 /* connected VMID/Bias for lower pops */
565                 if (widget->id == snd_soc_dapm_vmid)
566                         return 1;
567
568                 /* connected jack ? */
569                 if (widget->id == snd_soc_dapm_mic ||
570                     (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
571                         return 1;
572         }
573
574         list_for_each_entry(path, &widget->sources, list_sink) {
575                 if (path->walked)
576                         continue;
577
578                 if (path->source && path->connect) {
579                         path->walked = 1;
580                         con += is_connected_input_ep(path->source);
581                 }
582         }
583
584         return con;
585 }
586
587 /*
588  * Handler for generic register modifier widget.
589  */
590 int dapm_reg_event(struct snd_soc_dapm_widget *w,
591                    struct snd_kcontrol *kcontrol, int event)
592 {
593         unsigned int val;
594
595         if (SND_SOC_DAPM_EVENT_ON(event))
596                 val = w->on_val;
597         else
598                 val = w->off_val;
599
600         snd_soc_update_bits(w->codec, -(w->reg + 1),
601                             w->mask << w->shift, val << w->shift);
602
603         return 0;
604 }
605 EXPORT_SYMBOL_GPL(dapm_reg_event);
606
607 /* Standard power change method, used to apply power changes to most
608  * widgets.
609  */
610 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
611 {
612         int ret;
613
614         /* call any power change event handlers */
615         if (w->event)
616                 pr_debug("power %s event for %s flags %x\n",
617                          w->power ? "on" : "off",
618                          w->name, w->event_flags);
619
620         /* power up pre event */
621         if (w->power && w->event &&
622             (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
623                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
624                 if (ret < 0)
625                         return ret;
626         }
627
628         /* power down pre event */
629         if (!w->power && w->event &&
630             (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
631                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
632                 if (ret < 0)
633                         return ret;
634         }
635
636         /* Lower PGA volume to reduce pops */
637         if (w->id == snd_soc_dapm_pga && !w->power)
638                 dapm_set_pga(w, w->power);
639
640         dapm_update_bits(w);
641
642         /* Raise PGA volume to reduce pops */
643         if (w->id == snd_soc_dapm_pga && w->power)
644                 dapm_set_pga(w, w->power);
645
646         /* power up post event */
647         if (w->power && w->event &&
648             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
649                 ret = w->event(w,
650                                NULL, SND_SOC_DAPM_POST_PMU);
651                 if (ret < 0)
652                         return ret;
653         }
654
655         /* power down post event */
656         if (!w->power && w->event &&
657             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
658                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
659                 if (ret < 0)
660                         return ret;
661         }
662
663         return 0;
664 }
665
666 /* Generic check to see if a widget should be powered.
667  */
668 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
669 {
670         int in, out;
671
672         in = is_connected_input_ep(w);
673         dapm_clear_walk(w->codec);
674         out = is_connected_output_ep(w);
675         dapm_clear_walk(w->codec);
676         return out != 0 && in != 0;
677 }
678
679 /* Check to see if an ADC has power */
680 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
681 {
682         int in;
683
684         if (w->active) {
685                 in = is_connected_input_ep(w);
686                 dapm_clear_walk(w->codec);
687                 return in != 0;
688         } else {
689                 return dapm_generic_check_power(w);
690         }
691 }
692
693 /* Check to see if a DAC has power */
694 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
695 {
696         int out;
697
698         if (w->active) {
699                 out = is_connected_output_ep(w);
700                 dapm_clear_walk(w->codec);
701                 return out != 0;
702         } else {
703                 return dapm_generic_check_power(w);
704         }
705 }
706
707 /* Check to see if a power supply is needed */
708 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
709 {
710         struct snd_soc_dapm_path *path;
711         int power = 0;
712
713         /* Check if one of our outputs is connected */
714         list_for_each_entry(path, &w->sinks, list_source) {
715                 if (path->connected &&
716                     !path->connected(path->source, path->sink))
717                         continue;
718
719                 if (path->sink && path->sink->power_check &&
720                     path->sink->power_check(path->sink)) {
721                         power = 1;
722                         break;
723                 }
724         }
725
726         dapm_clear_walk(w->codec);
727
728         return power;
729 }
730
731 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
732                             struct snd_soc_dapm_widget *b,
733                             int sort[])
734 {
735         if (a->codec != b->codec)
736                 return (unsigned long)a - (unsigned long)b;
737         if (sort[a->id] != sort[b->id])
738                 return sort[a->id] - sort[b->id];
739         if (a->reg != b->reg)
740                 return a->reg - b->reg;
741
742         return 0;
743 }
744
745 /* Insert a widget in order into a DAPM power sequence. */
746 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
747                             struct list_head *list,
748                             int sort[])
749 {
750         struct snd_soc_dapm_widget *w;
751
752         list_for_each_entry(w, list, power_list)
753                 if (dapm_seq_compare(new_widget, w, sort) < 0) {
754                         list_add_tail(&new_widget->power_list, &w->power_list);
755                         return;
756                 }
757
758         list_add_tail(&new_widget->power_list, list);
759 }
760
761 /* Apply the coalesced changes from a DAPM sequence */
762 static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
763                                    struct list_head *pending)
764 {
765         struct snd_soc_dapm_widget *w;
766         int reg, power, ret;
767         unsigned int value = 0;
768         unsigned int mask = 0;
769         unsigned int cur_mask;
770
771         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
772                                power_list)->reg;
773
774         list_for_each_entry(w, pending, power_list) {
775                 cur_mask = 1 << w->shift;
776                 BUG_ON(reg != w->reg);
777
778                 if (w->invert)
779                         power = !w->power;
780                 else
781                         power = w->power;
782
783                 mask |= cur_mask;
784                 if (power)
785                         value |= cur_mask;
786
787                 pop_dbg(codec->pop_time,
788                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
789                         w->name, reg, value, mask);
790
791                 /* power up pre event */
792                 if (w->power && w->event &&
793                     (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
794                         pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
795                                 w->name);
796                         ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
797                         if (ret < 0)
798                                 pr_err("%s: pre event failed: %d\n",
799                                        w->name, ret);
800                 }
801
802                 /* power down pre event */
803                 if (!w->power && w->event &&
804                     (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
805                         pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
806                                 w->name);
807                         ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
808                         if (ret < 0)
809                                 pr_err("%s: pre event failed: %d\n",
810                                        w->name, ret);
811                 }
812
813                 /* Lower PGA volume to reduce pops */
814                 if (w->id == snd_soc_dapm_pga && !w->power)
815                         dapm_set_pga(w, w->power);
816         }
817
818         if (reg >= 0) {
819                 pop_dbg(codec->pop_time,
820                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
821                         value, mask, reg, codec->pop_time);
822                 pop_wait(codec->pop_time);
823                 snd_soc_update_bits(codec, reg, mask, value);
824         }
825
826         list_for_each_entry(w, pending, power_list) {
827                 /* Raise PGA volume to reduce pops */
828                 if (w->id == snd_soc_dapm_pga && w->power)
829                         dapm_set_pga(w, w->power);
830
831                 /* power up post event */
832                 if (w->power && w->event &&
833                     (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
834                         pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
835                                 w->name);
836                         ret = w->event(w,
837                                        NULL, SND_SOC_DAPM_POST_PMU);
838                         if (ret < 0)
839                                 pr_err("%s: post event failed: %d\n",
840                                        w->name, ret);
841                 }
842
843                 /* power down post event */
844                 if (!w->power && w->event &&
845                     (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
846                         pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
847                                 w->name);
848                         ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
849                         if (ret < 0)
850                                 pr_err("%s: post event failed: %d\n",
851                                        w->name, ret);
852                 }
853         }
854 }
855
856 /* Apply a DAPM power sequence.
857  *
858  * We walk over a pre-sorted list of widgets to apply power to.  In
859  * order to minimise the number of writes to the device required
860  * multiple widgets will be updated in a single write where possible.
861  * Currently anything that requires more than a single write is not
862  * handled.
863  */
864 static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
865                          int event, int sort[])
866 {
867         struct snd_soc_dapm_widget *w, *n;
868         LIST_HEAD(pending);
869         int cur_sort = -1;
870         int cur_reg = SND_SOC_NOPM;
871         int ret;
872
873         list_for_each_entry_safe(w, n, list, power_list) {
874                 ret = 0;
875
876                 /* Do we need to apply any queued changes? */
877                 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
878                         if (!list_empty(&pending))
879                                 dapm_seq_run_coalesced(codec, &pending);
880
881                         INIT_LIST_HEAD(&pending);
882                         cur_sort = -1;
883                         cur_reg = SND_SOC_NOPM;
884                 }
885
886                 switch (w->id) {
887                 case snd_soc_dapm_pre:
888                         if (!w->event)
889                                 list_for_each_entry_safe_continue(w, n, list,
890                                                                   power_list);
891
892                         if (event == SND_SOC_DAPM_STREAM_START)
893                                 ret = w->event(w,
894                                                NULL, SND_SOC_DAPM_PRE_PMU);
895                         else if (event == SND_SOC_DAPM_STREAM_STOP)
896                                 ret = w->event(w,
897                                                NULL, SND_SOC_DAPM_PRE_PMD);
898                         break;
899
900                 case snd_soc_dapm_post:
901                         if (!w->event)
902                                 list_for_each_entry_safe_continue(w, n, list,
903                                                                   power_list);
904
905                         if (event == SND_SOC_DAPM_STREAM_START)
906                                 ret = w->event(w,
907                                                NULL, SND_SOC_DAPM_POST_PMU);
908                         else if (event == SND_SOC_DAPM_STREAM_STOP)
909                                 ret = w->event(w,
910                                                NULL, SND_SOC_DAPM_POST_PMD);
911                         break;
912
913                 case snd_soc_dapm_input:
914                 case snd_soc_dapm_output:
915                 case snd_soc_dapm_hp:
916                 case snd_soc_dapm_mic:
917                 case snd_soc_dapm_line:
918                 case snd_soc_dapm_spk:
919                         /* No register support currently */
920                         ret = dapm_generic_apply_power(w);
921                         break;
922
923                 default:
924                         /* Queue it up for application */
925                         cur_sort = sort[w->id];
926                         cur_reg = w->reg;
927                         list_move(&w->power_list, &pending);
928                         break;
929                 }
930
931                 if (ret < 0)
932                         pr_err("Failed to apply widget power: %d\n",
933                                ret);
934         }
935
936         if (!list_empty(&pending))
937                 dapm_seq_run_coalesced(codec, &pending);
938 }
939
940 /*
941  * Scan each dapm widget for complete audio path.
942  * A complete path is a route that has valid endpoints i.e.:-
943  *
944  *  o DAC to output pin.
945  *  o Input Pin to ADC.
946  *  o Input pin to Output pin (bypass, sidetone)
947  *  o DAC to ADC (loopback).
948  */
949 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
950 {
951         struct snd_soc_device *socdev = codec->socdev;
952         struct snd_soc_dapm_widget *w;
953         LIST_HEAD(up_list);
954         LIST_HEAD(down_list);
955         int ret = 0;
956         int power;
957         int sys_power = 0;
958
959         /* Check which widgets we need to power and store them in
960          * lists indicating if they should be powered up or down.
961          */
962         list_for_each_entry(w, &codec->dapm_widgets, list) {
963                 switch (w->id) {
964                 case snd_soc_dapm_pre:
965                         dapm_seq_insert(w, &down_list, dapm_down_seq);
966                         break;
967                 case snd_soc_dapm_post:
968                         dapm_seq_insert(w, &up_list, dapm_up_seq);
969                         break;
970
971                 default:
972                         if (!w->power_check)
973                                 continue;
974
975                         /* If we're suspending then pull down all the 
976                          * power. */
977                         switch (event) {
978                         case SND_SOC_DAPM_STREAM_SUSPEND:
979                                 power = 0;
980                                 break;
981
982                         default:
983                                 power = w->power_check(w);
984                                 if (power)
985                                         sys_power = 1;
986                                 break;
987                         }
988
989                         if (w->power == power)
990                                 continue;
991
992                         if (power)
993                                 dapm_seq_insert(w, &up_list, dapm_up_seq);
994                         else
995                                 dapm_seq_insert(w, &down_list, dapm_down_seq);
996
997                         w->power = power;
998                         break;
999                 }
1000         }
1001
1002         /* If there are no DAPM widgets then try to figure out power from the
1003          * event type.
1004          */
1005         if (list_empty(&codec->dapm_widgets)) {
1006                 switch (event) {
1007                 case SND_SOC_DAPM_STREAM_START:
1008                 case SND_SOC_DAPM_STREAM_RESUME:
1009                         sys_power = 1;
1010                         break;
1011                 case SND_SOC_DAPM_STREAM_SUSPEND:
1012                         sys_power = 0;
1013                         break;
1014                 case SND_SOC_DAPM_STREAM_NOP:
1015                         switch (codec->bias_level) {
1016                                 case SND_SOC_BIAS_STANDBY:
1017                                 case SND_SOC_BIAS_OFF:
1018                                         sys_power = 0;
1019                                         break;
1020                                 default:
1021                                         sys_power = 1;
1022                                         break;
1023                         }
1024                         break;
1025                 default:
1026                         break;
1027                 }
1028         }
1029
1030         if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
1031                 ret = snd_soc_dapm_set_bias_level(socdev,
1032                                                   SND_SOC_BIAS_STANDBY);
1033                 if (ret != 0)
1034                         pr_err("Failed to turn on bias: %d\n", ret);
1035         }
1036
1037         /* If we're changing to all on or all off then prepare */
1038         if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
1039             (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
1040                 ret = snd_soc_dapm_set_bias_level(socdev,
1041                                                   SND_SOC_BIAS_PREPARE);
1042                 if (ret != 0)
1043                         pr_err("Failed to prepare bias: %d\n", ret);
1044         }
1045
1046         /* Power down widgets first; try to avoid amplifying pops. */
1047         dapm_seq_run(codec, &down_list, event, dapm_down_seq);
1048
1049         /* Now power up. */
1050         dapm_seq_run(codec, &up_list, event, dapm_up_seq);
1051
1052         /* If we just powered the last thing off drop to standby 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_STANDBY);
1056                 if (ret != 0)
1057                         pr_err("Failed to apply standby bias: %d\n", ret);
1058         }
1059
1060         /* If we're in standby and can support bias off then do that */
1061         if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
1062             codec->idle_bias_off) {
1063                 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1064                 if (ret != 0)
1065                         pr_err("Failed to turn off bias: %d\n", ret);
1066         }
1067
1068         /* If we just powered up then move to active bias */
1069         if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1070                 ret = snd_soc_dapm_set_bias_level(socdev,
1071                                                   SND_SOC_BIAS_ON);
1072                 if (ret != 0)
1073                         pr_err("Failed to apply active bias: %d\n", ret);
1074         }
1075
1076         pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1077                 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");