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