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