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