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