605c225c709a9b2c5a2f62f43aae88bf1ee65704
[firefly-linux-kernel-4.4.55.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                 pop_dbg(dapm->dev, card->pop_time,
922                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
923                         value, mask, reg, card->pop_time);
924                 pop_wait(card->pop_time);
925                 soc_widget_update_bits(w, reg, mask, value);
926         }
927
928         list_for_each_entry(w, pending, power_list) {
929                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
930                 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
931         }
932 }
933
934 /* Apply a DAPM power sequence.
935  *
936  * We walk over a pre-sorted list of widgets to apply power to.  In
937  * order to minimise the number of writes to the device required
938  * multiple widgets will be updated in a single write where possible.
939  * Currently anything that requires more than a single write is not
940  * handled.
941  */
942 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
943                          struct list_head *list, int event, bool power_up)
944 {
945         struct snd_soc_dapm_widget *w, *n;
946         LIST_HEAD(pending);
947         int cur_sort = -1;
948         int cur_subseq = -1;
949         int cur_reg = SND_SOC_NOPM;
950         struct snd_soc_dapm_context *cur_dapm = NULL;
951         int ret, i;
952         int *sort;
953
954         if (power_up)
955                 sort = dapm_up_seq;
956         else
957                 sort = dapm_down_seq;
958
959         list_for_each_entry_safe(w, n, list, power_list) {
960                 ret = 0;
961
962                 /* Do we need to apply any queued changes? */
963                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
964                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
965                         if (!list_empty(&pending))
966                                 dapm_seq_run_coalesced(cur_dapm, &pending);
967
968                         if (cur_dapm && cur_dapm->seq_notifier) {
969                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
970                                         if (sort[i] == cur_sort)
971                                                 cur_dapm->seq_notifier(cur_dapm,
972                                                                        i,
973                                                                        cur_subseq);
974                         }
975
976                         INIT_LIST_HEAD(&pending);
977                         cur_sort = -1;
978                         cur_subseq = -1;
979                         cur_reg = SND_SOC_NOPM;
980                         cur_dapm = NULL;
981                 }
982
983                 switch (w->id) {
984                 case snd_soc_dapm_pre:
985                         if (!w->event)
986                                 list_for_each_entry_safe_continue(w, n, list,
987                                                                   power_list);
988
989                         if (event == SND_SOC_DAPM_STREAM_START)
990                                 ret = w->event(w,
991                                                NULL, SND_SOC_DAPM_PRE_PMU);
992                         else if (event == SND_SOC_DAPM_STREAM_STOP)
993                                 ret = w->event(w,
994                                                NULL, SND_SOC_DAPM_PRE_PMD);
995                         break;
996
997                 case snd_soc_dapm_post:
998                         if (!w->event)
999                                 list_for_each_entry_safe_continue(w, n, list,
1000                                                                   power_list);
1001
1002                         if (event == SND_SOC_DAPM_STREAM_START)
1003                                 ret = w->event(w,
1004                                                NULL, SND_SOC_DAPM_POST_PMU);
1005                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1006                                 ret = w->event(w,
1007                                                NULL, SND_SOC_DAPM_POST_PMD);
1008                         break;
1009
1010                 default:
1011                         /* Queue it up for application */
1012                         cur_sort = sort[w->id];
1013                         cur_subseq = w->subseq;
1014                         cur_reg = w->reg;
1015                         cur_dapm = w->dapm;
1016                         list_move(&w->power_list, &pending);
1017                         break;
1018                 }
1019
1020                 if (ret < 0)
1021                         dev_err(w->dapm->dev,
1022                                 "Failed to apply widget power: %d\n", ret);
1023         }
1024
1025         if (!list_empty(&pending))
1026                 dapm_seq_run_coalesced(cur_dapm, &pending);
1027
1028         if (cur_dapm && cur_dapm->seq_notifier) {
1029                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1030                         if (sort[i] == cur_sort)
1031                                 cur_dapm->seq_notifier(cur_dapm,
1032                                                        i, cur_subseq);
1033         }
1034 }
1035
1036 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1037 {
1038         struct snd_soc_dapm_update *update = dapm->update;
1039         struct snd_soc_dapm_widget *w;
1040         int ret;
1041
1042         if (!update)
1043                 return;
1044
1045         w = update->widget;
1046
1047         if (w->event &&
1048             (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1049                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1050                 if (ret != 0)
1051                         pr_err("%s DAPM pre-event failed: %d\n",
1052                                w->name, ret);
1053         }
1054
1055         ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1056                                   update->val);
1057         if (ret < 0)
1058                 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1059
1060         if (w->event &&
1061             (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1062                 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1063                 if (ret != 0)
1064                         pr_err("%s DAPM post-event failed: %d\n",
1065                                w->name, ret);
1066         }
1067 }
1068
1069 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1070  * they're changing state.
1071  */
1072 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1073 {
1074         struct snd_soc_dapm_context *d = data;
1075         int ret;
1076
1077         /* If we're off and we're not supposed to be go into STANDBY */
1078         if (d->bias_level == SND_SOC_BIAS_OFF &&
1079             d->target_bias_level != SND_SOC_BIAS_OFF) {
1080                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1081                 if (ret != 0)
1082                         dev_err(d->dev,
1083                                 "Failed to turn on bias: %d\n", ret);
1084         }
1085
1086         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1087         if (d->bias_level != d->target_bias_level) {
1088                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1089                 if (ret != 0)
1090                         dev_err(d->dev,
1091                                 "Failed to prepare bias: %d\n", ret);
1092         }
1093 }
1094
1095 /* Async callback run prior to DAPM sequences - brings to their final
1096  * state.
1097  */
1098 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1099 {
1100         struct snd_soc_dapm_context *d = data;
1101         int ret;
1102
1103         /* If we just powered the last thing off drop to standby bias */
1104         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1105             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1106              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1107                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1108                 if (ret != 0)
1109                         dev_err(d->dev, "Failed to apply standby bias: %d\n",
1110                                 ret);
1111         }
1112
1113         /* If we're in standby and can support bias off then do that */
1114         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1115             d->target_bias_level == SND_SOC_BIAS_OFF) {
1116                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1117                 if (ret != 0)
1118                         dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1119         }
1120
1121         /* If we just powered up then move to active bias */
1122         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1123             d->target_bias_level == SND_SOC_BIAS_ON) {
1124                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1125                 if (ret != 0)
1126                         dev_err(d->dev, "Failed to apply active bias: %d\n",
1127                                 ret);
1128         }
1129 }
1130
1131 /*
1132  * Scan each dapm widget for complete audio path.
1133  * A complete path is a route that has valid endpoints i.e.:-
1134  *
1135  *  o DAC to output pin.
1136  *  o Input Pin to ADC.
1137  *  o Input pin to Output pin (bypass, sidetone)
1138  *  o DAC to ADC (loopback).
1139  */
1140 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1141 {
1142         struct snd_soc_card *card = dapm->card;
1143         struct snd_soc_dapm_widget *w;
1144         struct snd_soc_dapm_context *d;
1145         LIST_HEAD(up_list);
1146         LIST_HEAD(down_list);
1147         LIST_HEAD(async_domain);
1148         enum snd_soc_bias_level bias;
1149         int power;
1150
1151         trace_snd_soc_dapm_start(card);
1152
1153         list_for_each_entry(d, &card->dapm_list, list) {
1154                 if (d->n_widgets || d->codec == NULL) {
1155                         if (d->idle_bias_off)
1156                                 d->target_bias_level = SND_SOC_BIAS_OFF;
1157                         else
1158                                 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1159                 }
1160         }
1161
1162         /* Check which widgets we need to power and store them in
1163          * lists indicating if they should be powered up or down.
1164          */
1165         list_for_each_entry(w, &card->widgets, list) {
1166                 switch (w->id) {
1167                 case snd_soc_dapm_pre:
1168                         dapm_seq_insert(w, &down_list, false);
1169                         break;
1170                 case snd_soc_dapm_post:
1171                         dapm_seq_insert(w, &up_list, true);
1172                         break;
1173
1174                 default:
1175                         if (!w->power_check)
1176                                 continue;
1177
1178                         if (!w->force)
1179                                 power = w->power_check(w);
1180                         else
1181                                 power = 1;
1182
1183                         if (power) {
1184                                 d = w->dapm;
1185
1186                                 /* Supplies and micbiases only bring
1187                                  * the context up to STANDBY as unless
1188                                  * something else is active and
1189                                  * passing audio they generally don't
1190                                  * require full power.
1191                                  */
1192                                 switch (w->id) {
1193                                 case snd_soc_dapm_supply:
1194                                 case snd_soc_dapm_micbias:
1195                                         if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1196                                                 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1197                                         break;
1198                                 default:
1199                                         d->target_bias_level = SND_SOC_BIAS_ON;
1200                                         break;
1201                                 }
1202                         }
1203
1204                         if (w->power == power)
1205                                 continue;
1206
1207                         trace_snd_soc_dapm_widget_power(w, power);
1208
1209                         if (power)
1210                                 dapm_seq_insert(w, &up_list, true);
1211                         else
1212                                 dapm_seq_insert(w, &down_list, false);
1213
1214                         w->power = power;
1215                         break;
1216                 }
1217         }
1218
1219         /* If there are no DAPM widgets then try to figure out power from the
1220          * event type.
1221          */
1222         if (!dapm->n_widgets) {
1223                 switch (event) {
1224                 case SND_SOC_DAPM_STREAM_START:
1225                 case SND_SOC_DAPM_STREAM_RESUME:
1226                         dapm->target_bias_level = SND_SOC_BIAS_ON;
1227                         break;
1228                 case SND_SOC_DAPM_STREAM_STOP:
1229                         if (dapm->codec->active)
1230                                 dapm->target_bias_level = SND_SOC_BIAS_ON;
1231                         else
1232                                 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1233                         break;
1234                 case SND_SOC_DAPM_STREAM_SUSPEND:
1235                         dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1236                         break;
1237                 case SND_SOC_DAPM_STREAM_NOP:
1238                         dapm->target_bias_level = dapm->bias_level;
1239                         break;
1240                 default:
1241                         break;
1242                 }
1243         }
1244
1245         /* Force all contexts in the card to the same bias state */
1246         bias = SND_SOC_BIAS_OFF;
1247         list_for_each_entry(d, &card->dapm_list, list)
1248                 if (d->target_bias_level > bias)
1249                         bias = d->target_bias_level;
1250         list_for_each_entry(d, &card->dapm_list, list)
1251                 d->target_bias_level = bias;
1252
1253
1254         /* Run all the bias changes in parallel */
1255         list_for_each_entry(d, &dapm->card->dapm_list, list)
1256                 async_schedule_domain(dapm_pre_sequence_async, d,
1257                                         &async_domain);
1258         async_synchronize_full_domain(&async_domain);
1259
1260         /* Power down widgets first; try to avoid amplifying pops. */
1261         dapm_seq_run(dapm, &down_list, event, false);
1262
1263         dapm_widget_update(dapm);
1264
1265         /* Now power up. */
1266         dapm_seq_run(dapm, &up_list, event, true);
1267
1268         /* Run all the bias changes in parallel */
1269         list_for_each_entry(d, &dapm->card->dapm_list, list)
1270                 async_schedule_domain(dapm_post_sequence_async, d,
1271                                         &async_domain);
1272         async_synchronize_full_domain(&async_domain);
1273
1274         pop_dbg(dapm->dev, card->pop_time,
1275                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1276         pop_wait(card->pop_time);
1277
1278         trace_snd_soc_dapm_done(card);
1279
1280         return 0;
1281 }
1282
1283 #ifdef CONFIG_DEBUG_FS
1284 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1285 {
1286         file->private_data = inode->i_private;
1287         return 0;
1288 }
1289
1290 static ssize_t dapm_widget_power_read_file(struct file *file,
1291                                            char __user *user_buf,
1292                                            size_t count, loff_t *ppos)
1293 {
1294         struct snd_soc_dapm_widget *w = file->private_data;
1295         char *buf;
1296         int in, out;
1297         ssize_t ret;
1298         struct snd_soc_dapm_path *p = NULL;
1299
1300         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1301         if (!buf)
1302                 return -ENOMEM;
1303
1304         in = is_connected_input_ep(w);
1305         dapm_clear_walk(w->dapm);
1306         out = is_connected_output_ep(w);
1307         dapm_clear_walk(w->dapm);
1308
1309         ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
1310                        w->name, w->power ? "On" : "Off", in, out);
1311
1312         if (w->reg >= 0)
1313                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1314                                 " - R%d(0x%x) bit %d",
1315                                 w->reg, w->reg, w->shift);
1316
1317         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1318
1319         if (w->sname)
1320                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1321                                 w->sname,
1322                                 w->active ? "active" : "inactive");
1323
1324         list_for_each_entry(p, &w->sources, list_sink) {
1325                 if (p->connected && !p->connected(w, p->sink))
1326                         continue;
1327
1328                 if (p->connect)
1329                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1330                                         " in  \"%s\" \"%s\"\n",
1331                                         p->name ? p->name : "static",
1332                                         p->source->name);
1333         }
1334         list_for_each_entry(p, &w->sinks, list_source) {
1335                 if (p->connected && !p->connected(w, p->sink))
1336                         continue;
1337
1338                 if (p->connect)
1339                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1340                                         " out \"%s\" \"%s\"\n",
1341                                         p->name ? p->name : "static",
1342                                         p->sink->name);
1343         }
1344
1345         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1346
1347         kfree(buf);
1348         return ret;
1349 }
1350
1351 static const struct file_operations dapm_widget_power_fops = {
1352         .open = dapm_widget_power_open_file,
1353         .read = dapm_widget_power_read_file,
1354         .llseek = default_llseek,
1355 };
1356
1357 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1358 {
1359         file->private_data = inode->i_private;
1360         return 0;
1361 }
1362
1363 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1364                                    size_t count, loff_t *ppos)
1365 {
1366         struct snd_soc_dapm_context *dapm = file->private_data;
1367         char *level;
1368
1369         switch (dapm->bias_level) {
1370         case SND_SOC_BIAS_ON:
1371                 level = "On\n";
1372                 break;
1373         case SND_SOC_BIAS_PREPARE:
1374                 level = "Prepare\n";
1375                 break;
1376         case SND_SOC_BIAS_STANDBY:
1377                 level = "Standby\n";
1378                 break;
1379         case SND_SOC_BIAS_OFF:
1380                 level = "Off\n";
1381                 break;
1382         default:
1383                 BUG();
1384                 level = "Unknown\n";
1385                 break;
1386         }
1387
1388         return simple_read_from_buffer(user_buf, count, ppos, level,
1389                                        strlen(level));
1390 }
1391
1392 static const struct file_operations dapm_bias_fops = {
1393         .open = dapm_bias_open_file,
1394         .read = dapm_bias_read_file,
1395         .llseek = default_llseek,
1396 };
1397
1398 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1399         struct dentry *parent)
1400 {
1401         struct dentry *d;
1402
1403         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1404
1405         if (!dapm->debugfs_dapm) {
1406                 printk(KERN_WARNING
1407                        "Failed to create DAPM debugfs directory\n");
1408                 return;
1409         }
1410
1411         d = debugfs_create_file("bias_level", 0444,
1412                                 dapm->debugfs_dapm, dapm,
1413                                 &dapm_bias_fops);
1414         if (!d)
1415                 dev_warn(dapm->dev,
1416                          "ASoC: Failed to create bias level debugfs file\n");
1417 }
1418
1419 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1420 {
1421         struct snd_soc_dapm_context *dapm = w->dapm;
1422         struct dentry *d;
1423
1424         if (!dapm->debugfs_dapm || !w->name)
1425                 return;
1426
1427         d = debugfs_create_file(w->name, 0444,
1428                                 dapm->debugfs_dapm, w,
1429                                 &dapm_widget_power_fops);
1430         if (!d)
1431                 dev_warn(w->dapm->dev,
1432                         "ASoC: Failed to create %s debugfs file\n",
1433                         w->name);
1434 }
1435
1436 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1437 {
1438         debugfs_remove_recursive(dapm->debugfs_dapm);
1439 }
1440
1441 #else
1442 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1443         struct dentry *parent)
1444 {
1445 }
1446
1447 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1448 {
1449 }
1450
1451 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1452 {
1453 }
1454
1455 #endif
1456
1457 /* test and update the power status of a mux widget */
1458 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1459                                  struct snd_kcontrol *kcontrol, int change,
1460                                  int mux, struct soc_enum *e)
1461 {
1462         struct snd_soc_dapm_path *path;
1463         int found = 0;
1464
1465         if (widget->id != snd_soc_dapm_mux &&
1466             widget->id != snd_soc_dapm_virt_mux &&
1467             widget->id != snd_soc_dapm_value_mux)
1468                 return -ENODEV;
1469
1470         if (!change)
1471                 return 0;
1472
1473         /* find dapm widget path assoc with kcontrol */
1474         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1475                 if (path->kcontrol != kcontrol)
1476                         continue;
1477
1478                 if (!path->name || !e->texts[mux])
1479                         continue;
1480
1481                 found = 1;
1482                 /* we now need to match the string in the enum to the path */
1483                 if (!(strcmp(path->name, e->texts[mux])))
1484                         path->connect = 1; /* new connection */
1485                 else
1486                         path->connect = 0; /* old connection must be powered down */
1487         }
1488
1489         if (found)
1490                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1491
1492         return 0;
1493 }
1494
1495 /* test and update the power status of a mixer or switch widget */
1496 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1497                                    struct snd_kcontrol *kcontrol, int connect)
1498 {
1499         struct snd_soc_dapm_path *path;
1500         int found = 0;
1501
1502         if (widget->id != snd_soc_dapm_mixer &&
1503             widget->id != snd_soc_dapm_mixer_named_ctl &&
1504             widget->id != snd_soc_dapm_switch)
1505                 return -ENODEV;
1506
1507         /* find dapm widget path assoc with kcontrol */
1508         list_for_each_entry(path, &widget->dapm->card->paths, list) {
1509                 if (path->kcontrol != kcontrol)
1510                         continue;
1511
1512                 /* found, now check type */
1513                 found = 1;
1514                 path->connect = connect;
1515                 break;
1516         }
1517
1518         if (found)
1519                 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1520
1521         return 0;
1522 }
1523
1524 /* show dapm widget status in sys fs */
1525 static ssize_t dapm_widget_show(struct device *dev,
1526         struct device_attribute *attr, char *buf)
1527 {
1528         struct snd_soc_pcm_runtime *rtd =
1529                         container_of(dev, struct snd_soc_pcm_runtime, dev);
1530         struct snd_soc_codec *codec =rtd->codec;
1531         struct snd_soc_dapm_widget *w;
1532         int count = 0;
1533         char *state = "not set";
1534
1535         list_for_each_entry(w, &codec->card->widgets, list) {
1536                 if (w->dapm != &codec->dapm)
1537                         continue;
1538
1539                 /* only display widgets that burnm power */
1540                 switch (w->id) {
1541                 case snd_soc_dapm_hp:
1542                 case snd_soc_dapm_mic:
1543                 case snd_soc_dapm_spk:
1544                 case snd_soc_dapm_line:
1545                 case snd_soc_dapm_micbias:
1546                 case snd_soc_dapm_dac:
1547                 case snd_soc_dapm_adc:
1548                 case snd_soc_dapm_pga:
1549                 case snd_soc_dapm_out_drv:
1550                 case snd_soc_dapm_mixer:
1551                 case snd_soc_dapm_mixer_named_ctl:
1552                 case snd_soc_dapm_supply:
1553                         if (w->name)
1554                                 count += sprintf(buf + count, "%s: %s\n",
1555                                         w->name, w->power ? "On":"Off");
1556                 break;
1557                 default:
1558                 break;
1559                 }
1560         }
1561
1562         switch (codec->dapm.bias_level) {
1563         case SND_SOC_BIAS_ON:
1564                 state = "On";
1565                 break;
1566         case SND_SOC_BIAS_PREPARE:
1567                 state = "Prepare";
1568                 break;
1569         case SND_SOC_BIAS_STANDBY:
1570                 state = "Standby";
1571                 break;
1572         case SND_SOC_BIAS_OFF:
1573                 state = "Off";
1574                 break;
1575         }
1576         count += sprintf(buf + count, "PM State: %s\n", state);
1577
1578         return count;
1579 }
1580
1581 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1582
1583 int snd_soc_dapm_sys_add(struct device *dev)
1584 {
1585         return device_create_file(dev, &dev_attr_dapm_widget);
1586 }
1587
1588 static void snd_soc_dapm_sys_remove(struct device *dev)
1589 {
1590         device_remove_file(dev, &dev_attr_dapm_widget);
1591 }
1592
1593 /* free all dapm widgets and resources */
1594 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1595 {
1596         struct snd_soc_dapm_widget *w, *next_w;
1597         struct snd_soc_dapm_path *p, *next_p;
1598
1599         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1600                 if (w->dapm != dapm)
1601                         continue;
1602                 list_del(&w->list);
1603                 /*
1604                  * remove source and sink paths associated to this widget.
1605                  * While removing the path, remove reference to it from both
1606                  * source and sink widgets so that path is removed only once.
1607                  */
1608                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1609                         list_del(&p->list_sink);
1610                         list_del(&p->list_source);
1611                         list_del(&p->list);
1612                         kfree(p->long_name);
1613                         kfree(p);
1614                 }
1615                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1616                         list_del(&p->list_sink);
1617                         list_del(&p->list_source);
1618                         list_del(&p->list);
1619                         kfree(p->long_name);
1620                         kfree(p);
1621                 }
1622                 kfree(w->kcontrols);
1623                 kfree(w->name);
1624                 kfree(w);
1625         }
1626 }
1627
1628 static struct snd_soc_dapm_widget *dapm_find_widget(
1629                         struct snd_soc_dapm_context *dapm, const char *pin,
1630                         bool search_other_contexts)
1631 {
1632         struct snd_soc_dapm_widget *w;
1633         struct snd_soc_dapm_widget *fallback = NULL;
1634
1635         list_for_each_entry(w, &dapm->card->widgets, list) {
1636                 if (!strcmp(w->name, pin)) {
1637                         if (w->dapm == dapm)
1638                                 return w;
1639                         else
1640                                 fallback = w;
1641                 }
1642         }
1643
1644         if (search_other_contexts)
1645                 return fallback;
1646
1647         return NULL;
1648 }
1649
1650 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1651                                 const char *pin, int status)
1652 {
1653         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1654
1655         if (!w) {
1656                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1657                 return -EINVAL;
1658         }
1659
1660         w->connected = status;
1661         if (status == 0)
1662                 w->force = 0;
1663
1664         return 0;
1665 }
1666
1667 /**
1668  * snd_soc_dapm_sync - scan and power dapm paths
1669  * @dapm: DAPM context
1670  *
1671  * Walks all dapm audio paths and powers widgets according to their
1672  * stream or path usage.
1673  *
1674  * Returns 0 for success.
1675  */
1676 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1677 {
1678         return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1679 }
1680 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1681
1682 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1683                                   const struct snd_soc_dapm_route *route)
1684 {
1685         struct snd_soc_dapm_path *path;
1686         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1687         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1688         const char *sink;
1689         const char *control = route->control;
1690         const char *source;
1691         char prefixed_sink[80];
1692         char prefixed_source[80];
1693         int ret = 0;
1694
1695         if (dapm->codec && dapm->codec->name_prefix) {
1696                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1697                          dapm->codec->name_prefix, route->sink);
1698                 sink = prefixed_sink;
1699                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1700                          dapm->codec->name_prefix, route->source);
1701                 source = prefixed_source;
1702         } else {
1703                 sink = route->sink;
1704                 source = route->source;
1705         }
1706
1707         /*
1708          * find src and dest widgets over all widgets but favor a widget from
1709          * current DAPM context
1710          */
1711         list_for_each_entry(w, &dapm->card->widgets, list) {
1712                 if (!wsink && !(strcmp(w->name, sink))) {
1713                         wtsink = w;
1714                         if (w->dapm == dapm)
1715                                 wsink = w;
1716                         continue;
1717                 }
1718                 if (!wsource && !(strcmp(w->name, source))) {
1719                         wtsource = w;
1720                         if (w->dapm == dapm)
1721                                 wsource = w;
1722                 }
1723         }
1724         /* use widget from another DAPM context if not found from this */
1725         if (!wsink)
1726                 wsink = wtsink;
1727         if (!wsource)
1728                 wsource = wtsource;
1729
1730         if (wsource == NULL || wsink == NULL)
1731                 return -ENODEV;
1732
1733         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1734         if (!path)
1735                 return -ENOMEM;
1736
1737         path->source = wsource;
1738         path->sink = wsink;
1739         path->connected = route->connected;
1740         INIT_LIST_HEAD(&path->list);
1741         INIT_LIST_HEAD(&path->list_source);
1742         INIT_LIST_HEAD(&path->list_sink);
1743
1744         /* check for external widgets */
1745         if (wsink->id == snd_soc_dapm_input) {
1746                 if (wsource->id == snd_soc_dapm_micbias ||
1747                         wsource->id == snd_soc_dapm_mic ||
1748                         wsource->id == snd_soc_dapm_line ||
1749                         wsource->id == snd_soc_dapm_output)
1750                         wsink->ext = 1;
1751         }
1752         if (wsource->id == snd_soc_dapm_output) {
1753                 if (wsink->id == snd_soc_dapm_spk ||
1754                         wsink->id == snd_soc_dapm_hp ||
1755                         wsink->id == snd_soc_dapm_line ||
1756                         wsink->id == snd_soc_dapm_input)
1757                         wsource->ext = 1;
1758         }
1759
1760         /* connect static paths */
1761         if (control == NULL) {
1762                 list_add(&path->list, &dapm->card->paths);
1763                 list_add(&path->list_sink, &wsink->sources);
1764                 list_add(&path->list_source, &wsource->sinks);
1765                 path->connect = 1;
1766                 return 0;
1767         }
1768
1769         /* connect dynamic paths */
1770         switch (wsink->id) {
1771         case snd_soc_dapm_adc:
1772         case snd_soc_dapm_dac:
1773         case snd_soc_dapm_pga:
1774         case snd_soc_dapm_out_drv:
1775         case snd_soc_dapm_input:
1776         case snd_soc_dapm_output:
1777         case snd_soc_dapm_micbias:
1778         case snd_soc_dapm_vmid:
1779         case snd_soc_dapm_pre:
1780         case snd_soc_dapm_post:
1781         case snd_soc_dapm_supply:
1782         case snd_soc_dapm_aif_in:
1783         case snd_soc_dapm_aif_out:
1784                 list_add(&path->list, &dapm->card->paths);
1785                 list_add(&path->list_sink, &wsink->sources);
1786                 list_add(&path->list_source, &wsource->sinks);
1787                 path->connect = 1;
1788                 return 0;
1789         case snd_soc_dapm_mux:
1790         case snd_soc_dapm_virt_mux:
1791         case snd_soc_dapm_value_mux:
1792                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1793                         &wsink->kcontrol_news[0]);
1794                 if (ret != 0)
1795                         goto err;
1796                 break;
1797         case snd_soc_dapm_switch:
1798         case snd_soc_dapm_mixer:
1799         case snd_soc_dapm_mixer_named_ctl:
1800                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1801                 if (ret != 0)
1802                         goto err;
1803                 break;
1804         case snd_soc_dapm_hp:
1805         case snd_soc_dapm_mic:
1806         case snd_soc_dapm_line:
1807         case snd_soc_dapm_spk:
1808                 list_add(&path->list, &dapm->card->paths);
1809                 list_add(&path->list_sink, &wsink->sources);
1810                 list_add(&path->list_source, &wsource->sinks);
1811                 path->connect = 0;
1812                 return 0;
1813         }
1814         return 0;
1815
1816 err:
1817         dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1818                  source, control, sink);
1819         kfree(path);
1820         return ret;
1821 }
1822
1823 /**
1824  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1825  * @dapm: DAPM context
1826  * @route: audio routes
1827  * @num: number of routes
1828  *
1829  * Connects 2 dapm widgets together via a named audio path. The sink is
1830  * the widget receiving the audio signal, whilst the source is the sender
1831  * of the audio signal.
1832  *
1833  * Returns 0 for success else error. On error all resources can be freed
1834  * with a call to snd_soc_card_free().
1835  */
1836 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1837                             const struct snd_soc_dapm_route *route, int num)
1838 {
1839         int i, ret;
1840
1841         for (i = 0; i < num; i++) {
1842                 ret = snd_soc_dapm_add_route(dapm, route);
1843                 if (ret < 0) {
1844                         dev_err(dapm->dev, "Failed to add route %s->%s\n",
1845                                 route->source, route->sink);
1846                         return ret;
1847                 }
1848                 route++;
1849         }
1850
1851         return 0;
1852 }
1853 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1854
1855 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1856                                    const struct snd_soc_dapm_route *route)
1857 {
1858         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1859                                                               route->source,
1860                                                               true);
1861         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1862                                                             route->sink,
1863                                                             true);
1864         struct snd_soc_dapm_path *path;
1865         int count = 0;
1866
1867         if (!source) {
1868                 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1869                         route->source);
1870                 return -ENODEV;
1871         }
1872
1873         if (!sink) {
1874                 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1875                         route->sink);
1876                 return -ENODEV;
1877         }
1878
1879         if (route->control || route->connected)
1880                 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1881                          route->source, route->sink);
1882
1883         list_for_each_entry(path, &source->sinks, list_source) {
1884                 if (path->sink == sink) {
1885                         path->weak = 1;
1886                         count++;
1887                 }
1888         }
1889
1890         if (count == 0)
1891                 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1892                         route->source, route->sink);
1893         if (count > 1)
1894                 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1895                          count, route->source, route->sink);
1896
1897         return 0;
1898 }
1899
1900 /**
1901  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1902  * @dapm: DAPM context
1903  * @route: audio routes
1904  * @num: number of routes
1905  *
1906  * Mark existing routes matching those specified in the passed array
1907  * as being weak, meaning that they are ignored for the purpose of
1908  * power decisions.  The main intended use case is for sidetone paths
1909  * which couple audio between other independent paths if they are both
1910  * active in order to make the combination work better at the user
1911  * level but which aren't intended to be "used".
1912  *
1913  * Note that CODEC drivers should not use this as sidetone type paths
1914  * can frequently also be used as bypass paths.
1915  */
1916 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1917                              const struct snd_soc_dapm_route *route, int num)
1918 {
1919         int i, err;
1920         int ret = 0;
1921
1922         for (i = 0; i < num; i++) {
1923                 err = snd_soc_dapm_weak_route(dapm, route);
1924                 if (err)
1925                         ret = err;
1926                 route++;
1927         }
1928
1929         return ret;
1930 }
1931 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1932
1933 /**
1934  * snd_soc_dapm_new_widgets - add new dapm widgets
1935  * @dapm: DAPM context
1936  *
1937  * Checks the codec for any new dapm widgets and creates them if found.
1938  *
1939  * Returns 0 for success.
1940  */
1941 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1942 {
1943         struct snd_soc_dapm_widget *w;
1944         unsigned int val;
1945
1946         list_for_each_entry(w, &dapm->card->widgets, list)
1947         {
1948                 if (w->new)
1949                         continue;
1950
1951                 if (w->num_kcontrols) {
1952                         w->kcontrols = kzalloc(w->num_kcontrols *
1953                                                 sizeof(struct snd_kcontrol *),
1954                                                 GFP_KERNEL);
1955                         if (!w->kcontrols)
1956                                 return -ENOMEM;
1957                 }
1958
1959                 switch(w->id) {
1960                 case snd_soc_dapm_switch:
1961                 case snd_soc_dapm_mixer:
1962                 case snd_soc_dapm_mixer_named_ctl:
1963                         w->power_check = dapm_generic_check_power;
1964                         dapm_new_mixer(w);
1965                         break;
1966                 case snd_soc_dapm_mux:
1967                 case snd_soc_dapm_virt_mux:
1968                 case snd_soc_dapm_value_mux:
1969                         w->power_check = dapm_generic_check_power;
1970                         dapm_new_mux(w);
1971                         break;
1972                 case snd_soc_dapm_adc:
1973                 case snd_soc_dapm_aif_out:
1974                         w->power_check = dapm_adc_check_power;
1975                         break;
1976                 case snd_soc_dapm_dac:
1977                 case snd_soc_dapm_aif_in:
1978                         w->power_check = dapm_dac_check_power;
1979                         break;
1980                 case snd_soc_dapm_pga:
1981                 case snd_soc_dapm_out_drv:
1982                         w->power_check = dapm_generic_check_power;
1983                         dapm_new_pga(w);
1984                         break;
1985                 case snd_soc_dapm_input:
1986                 case snd_soc_dapm_output:
1987                 case snd_soc_dapm_micbias:
1988                 case snd_soc_dapm_spk:
1989                 case snd_soc_dapm_hp:
1990                 case snd_soc_dapm_mic:
1991                 case snd_soc_dapm_line:
1992                         w->power_check = dapm_generic_check_power;
1993                         break;
1994                 case snd_soc_dapm_supply:
1995                         w->power_check = dapm_supply_check_power;
1996                 case snd_soc_dapm_vmid:
1997                 case snd_soc_dapm_pre:
1998                 case snd_soc_dapm_post:
1999                         break;
2000                 }
2001
2002                 /* Read the initial power state from the device */
2003                 if (w->reg >= 0) {
2004                         val = soc_widget_read(w, w->reg);
2005                         val &= 1 << w->shift;
2006                         if (w->invert)
2007                                 val = !val;
2008
2009                         if (val)
2010                                 w->power = 1;
2011                 }
2012
2013                 w->new = 1;
2014
2015                 dapm_debugfs_add_widget(w);
2016         }
2017
2018         dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2019         return 0;
2020 }
2021 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2022
2023 /**
2024  * snd_soc_dapm_get_volsw - dapm mixer get callback
2025  * @kcontrol: mixer control
2026  * @ucontrol: control element information
2027  *
2028  * Callback to get the value of a dapm mixer control.
2029  *
2030  * Returns 0 for success.
2031  */
2032 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2033         struct snd_ctl_elem_value *ucontrol)
2034 {
2035         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2036         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2037         struct soc_mixer_control *mc =
2038                 (struct soc_mixer_control *)kcontrol->private_value;
2039         unsigned int reg = mc->reg;
2040         unsigned int shift = mc->shift;
2041         unsigned int rshift = mc->rshift;
2042         int max = mc->max;
2043         unsigned int invert = mc->invert;
2044         unsigned int mask = (1 << fls(max)) - 1;
2045
2046         ucontrol->value.integer.value[0] =
2047                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2048         if (shift != rshift)
2049                 ucontrol->value.integer.value[1] =
2050                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2051         if (invert) {
2052                 ucontrol->value.integer.value[0] =
2053                         max - ucontrol->value.integer.value[0];
2054                 if (shift != rshift)
2055                         ucontrol->value.integer.value[1] =
2056                                 max - ucontrol->value.integer.value[1];
2057         }
2058
2059         return 0;
2060 }
2061 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2062
2063 /**
2064  * snd_soc_dapm_put_volsw - dapm mixer set callback
2065  * @kcontrol: mixer control
2066  * @ucontrol: control element information
2067  *
2068  * Callback to set the value of a dapm mixer control.
2069  *
2070  * Returns 0 for success.
2071  */
2072 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2073         struct snd_ctl_elem_value *ucontrol)
2074 {
2075         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2076         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2077         struct snd_soc_codec *codec = widget->codec;
2078         struct soc_mixer_control *mc =
2079                 (struct soc_mixer_control *)kcontrol->private_value;
2080         unsigned int reg = mc->reg;
2081         unsigned int shift = mc->shift;
2082         int max = mc->max;
2083         unsigned int mask = (1 << fls(max)) - 1;
2084         unsigned int invert = mc->invert;
2085         unsigned int val;
2086         int connect, change;
2087         struct snd_soc_dapm_update update;
2088         int wi;
2089
2090         val = (ucontrol->value.integer.value[0] & mask);
2091
2092         if (invert)
2093                 val = max - val;
2094         mask = mask << shift;
2095         val = val << shift;
2096
2097         if (val)
2098                 /* new connection */
2099                 connect = invert ? 0 : 1;
2100         else
2101                 /* old connection must be powered down */
2102                 connect = invert ? 1 : 0;
2103
2104         mutex_lock(&codec->mutex);
2105
2106         change = snd_soc_test_bits(widget->codec, reg, mask, val);
2107         if (change) {
2108                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2109                         widget = wlist->widgets[wi];
2110
2111                         widget->value = val;
2112
2113                         update.kcontrol = kcontrol;
2114                         update.widget = widget;
2115                         update.reg = reg;
2116                         update.mask = mask;
2117                         update.val = val;
2118                         widget->dapm->update = &update;
2119
2120                         dapm_mixer_update_power(widget, kcontrol, connect);
2121
2122                         widget->dapm->update = NULL;
2123                 }
2124         }
2125
2126         mutex_unlock(&codec->mutex);
2127         return 0;
2128 }
2129 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2130
2131 /**
2132  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2133  * @kcontrol: mixer control
2134  * @ucontrol: control element information
2135  *
2136  * Callback to get the value of a dapm enumerated double mixer control.
2137  *
2138  * Returns 0 for success.
2139  */
2140 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2141         struct snd_ctl_elem_value *ucontrol)
2142 {
2143         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2144         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2145         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2146         unsigned int val, bitmask;
2147
2148         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2149                 ;
2150         val = snd_soc_read(widget->codec, e->reg);
2151         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2152         if (e->shift_l != e->shift_r)
2153                 ucontrol->value.enumerated.item[1] =
2154                         (val >> e->shift_r) & (bitmask - 1);
2155
2156         return 0;
2157 }
2158 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2159
2160 /**
2161  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2162  * @kcontrol: mixer control
2163  * @ucontrol: control element information
2164  *
2165  * Callback to set the value of a dapm enumerated double mixer control.
2166  *
2167  * Returns 0 for success.
2168  */
2169 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2170         struct snd_ctl_elem_value *ucontrol)
2171 {
2172         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2173         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2174         struct snd_soc_codec *codec = widget->codec;
2175         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2176         unsigned int val, mux, change;
2177         unsigned int mask, bitmask;
2178         struct snd_soc_dapm_update update;
2179         int wi;
2180
2181         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2182                 ;
2183         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2184                 return -EINVAL;
2185         mux = ucontrol->value.enumerated.item[0];
2186         val = mux << e->shift_l;
2187         mask = (bitmask - 1) << e->shift_l;
2188         if (e->shift_l != e->shift_r) {
2189                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2190                         return -EINVAL;
2191                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2192                 mask |= (bitmask - 1) << e->shift_r;
2193         }
2194
2195         mutex_lock(&codec->mutex);
2196
2197         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2198         if (change) {
2199                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2200                         widget = wlist->widgets[wi];
2201
2202                         widget->value = val;
2203
2204                         update.kcontrol = kcontrol;
2205                         update.widget = widget;
2206                         update.reg = e->reg;
2207                         update.mask = mask;
2208                         update.val = val;
2209                         widget->dapm->update = &update;
2210
2211                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2212
2213                         widget->dapm->update = NULL;
2214                 }
2215         }
2216
2217         mutex_unlock(&codec->mutex);
2218         return change;
2219 }
2220 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2221
2222 /**
2223  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2224  * @kcontrol: mixer control
2225  * @ucontrol: control element information
2226  *
2227  * Returns 0 for success.
2228  */
2229 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2230                                struct snd_ctl_elem_value *ucontrol)
2231 {
2232         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2233         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2234
2235         ucontrol->value.enumerated.item[0] = widget->value;
2236
2237         return 0;
2238 }
2239 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2240
2241 /**
2242  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2243  * @kcontrol: mixer control
2244  * @ucontrol: control element information
2245  *
2246  * Returns 0 for success.
2247  */
2248 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2249                                struct snd_ctl_elem_value *ucontrol)
2250 {
2251         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2252         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2253         struct snd_soc_codec *codec = widget->codec;
2254         struct soc_enum *e =
2255                 (struct soc_enum *)kcontrol->private_value;
2256         int change;
2257         int ret = 0;
2258         int wi;
2259
2260         if (ucontrol->value.enumerated.item[0] >= e->max)
2261                 return -EINVAL;
2262
2263         mutex_lock(&codec->mutex);
2264
2265         change = widget->value != ucontrol->value.enumerated.item[0];
2266         if (change) {
2267                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2268                         widget = wlist->widgets[wi];
2269
2270                         widget->value = ucontrol->value.enumerated.item[0];
2271
2272                         dapm_mux_update_power(widget, kcontrol, change,
2273                                               widget->value, e);
2274                 }
2275         }
2276
2277         mutex_unlock(&codec->mutex);
2278         return ret;
2279 }
2280 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2281
2282 /**
2283  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2284  *                                      callback
2285  * @kcontrol: mixer control
2286  * @ucontrol: control element information
2287  *
2288  * Callback to get the value of a dapm semi enumerated double mixer control.
2289  *
2290  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2291  * used for handling bitfield coded enumeration for example.
2292  *
2293  * Returns 0 for success.
2294  */
2295 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2296         struct snd_ctl_elem_value *ucontrol)
2297 {
2298         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2299         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2300         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2301         unsigned int reg_val, val, mux;
2302
2303         reg_val = snd_soc_read(widget->codec, e->reg);
2304         val = (reg_val >> e->shift_l) & e->mask;
2305         for (mux = 0; mux < e->max; mux++) {
2306                 if (val == e->values[mux])
2307                         break;
2308         }
2309         ucontrol->value.enumerated.item[0] = mux;
2310         if (e->shift_l != e->shift_r) {
2311                 val = (reg_val >> e->shift_r) & e->mask;
2312                 for (mux = 0; mux < e->max; mux++) {
2313                         if (val == e->values[mux])
2314                                 break;
2315                 }
2316                 ucontrol->value.enumerated.item[1] = mux;
2317         }
2318
2319         return 0;
2320 }
2321 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2322
2323 /**
2324  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2325  *                                      callback
2326  * @kcontrol: mixer control
2327  * @ucontrol: control element information
2328  *
2329  * Callback to set the value of a dapm semi enumerated double mixer control.
2330  *
2331  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2332  * used for handling bitfield coded enumeration for example.
2333  *
2334  * Returns 0 for success.
2335  */
2336 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2337         struct snd_ctl_elem_value *ucontrol)
2338 {
2339         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2340         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2341         struct snd_soc_codec *codec = widget->codec;
2342         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2343         unsigned int val, mux, change;
2344         unsigned int mask;
2345         struct snd_soc_dapm_update update;
2346         int wi;
2347
2348         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2349                 return -EINVAL;
2350         mux = ucontrol->value.enumerated.item[0];
2351         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2352         mask = e->mask << e->shift_l;
2353         if (e->shift_l != e->shift_r) {
2354                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2355                         return -EINVAL;
2356                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2357                 mask |= e->mask << e->shift_r;
2358         }
2359
2360         mutex_lock(&codec->mutex);
2361
2362         change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2363         if (change) {
2364                 for (wi = 0; wi < wlist->num_widgets; wi++) {
2365                         widget = wlist->widgets[wi];
2366
2367                         widget->value = val;
2368
2369                         update.kcontrol = kcontrol;
2370                         update.widget = widget;
2371                         update.reg = e->reg;
2372                         update.mask = mask;
2373                         update.val = val;
2374                         widget->dapm->update = &update;
2375
2376                         dapm_mux_update_power(widget, kcontrol, change, mux, e);
2377
2378                         widget->dapm->update = NULL;
2379                 }
2380         }
2381
2382         mutex_unlock(&codec->mutex);
2383         return change;
2384 }
2385 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2386
2387 /**
2388  * snd_soc_dapm_info_pin_switch - Info for a pin switch
2389  *
2390  * @kcontrol: mixer control
2391  * @uinfo: control element information
2392  *
2393  * Callback to provide information about a pin switch control.
2394  */
2395 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2396                                  struct snd_ctl_elem_info *uinfo)
2397 {
2398         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2399         uinfo->count = 1;
2400         uinfo->value.integer.min = 0;
2401         uinfo->value.integer.max = 1;
2402
2403         return 0;
2404 }
2405 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2406
2407 /**
2408  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2409  *
2410  * @kcontrol: mixer control
2411  * @ucontrol: Value
2412  */
2413 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2414                                 struct snd_ctl_elem_value *ucontrol)
2415 {
2416         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2417         const char *pin = (const char *)kcontrol->private_value;
2418
2419         mutex_lock(&codec->mutex);
2420
2421         ucontrol->value.integer.value[0] =
2422                 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2423
2424         mutex_unlock(&codec->mutex);
2425
2426         return 0;
2427 }
2428 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2429
2430 /**
2431  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2432  *
2433  * @kcontrol: mixer control
2434  * @ucontrol: Value
2435  */
2436 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2437                                 struct snd_ctl_elem_value *ucontrol)
2438 {
2439         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2440         const char *pin = (const char *)kcontrol->private_value;
2441
2442         mutex_lock(&codec->mutex);
2443
2444         if (ucontrol->value.integer.value[0])
2445                 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2446         else
2447                 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2448
2449         snd_soc_dapm_sync(&codec->dapm);
2450
2451         mutex_unlock(&codec->mutex);
2452
2453         return 0;
2454 }
2455 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2456
2457 /**
2458  * snd_soc_dapm_new_control - create new dapm control
2459  * @dapm: DAPM context
2460  * @widget: widget template
2461  *
2462  * Creates a new dapm control based upon the template.
2463  *
2464  * Returns 0 for success else error.
2465  */
2466 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2467         const struct snd_soc_dapm_widget *widget)
2468 {
2469         struct snd_soc_dapm_widget *w;
2470         size_t name_len;
2471
2472         if ((w = dapm_cnew_widget(widget)) == NULL)
2473                 return -ENOMEM;
2474
2475         name_len = strlen(widget->name) + 1;
2476         if (dapm->codec && dapm->codec->name_prefix)
2477                 name_len += 1 + strlen(dapm->codec->name_prefix);
2478         w->name = kmalloc(name_len, GFP_KERNEL);
2479         if (w->name == NULL) {
2480                 kfree(w);
2481                 return -ENOMEM;
2482         }
2483         if (dapm->codec && dapm->codec->name_prefix)
2484                 snprintf(w->name, name_len, "%s %s",
2485                         dapm->codec->name_prefix, widget->name);
2486         else
2487                 snprintf(w->name, name_len, "%s", widget->name);
2488
2489         dapm->n_widgets++;
2490         w->dapm = dapm;
2491         w->codec = dapm->codec;
2492         INIT_LIST_HEAD(&w->sources);
2493         INIT_LIST_HEAD(&w->sinks);
2494         INIT_LIST_HEAD(&w->list);
2495         list_add(&w->list, &dapm->card->widgets);
2496
2497         /* machine layer set ups unconnected pins and insertions */
2498         w->connected = 1;
2499         return 0;
2500 }
2501 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2502
2503 /**
2504  * snd_soc_dapm_new_controls - create new dapm controls
2505  * @dapm: DAPM context
2506  * @widget: widget array
2507  * @num: number of widgets
2508  *
2509  * Creates new DAPM controls based upon the templates.
2510  *
2511  * Returns 0 for success else error.
2512  */
2513 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2514         const struct snd_soc_dapm_widget *widget,
2515         int num)
2516 {
2517         int i, ret;
2518
2519         for (i = 0; i < num; i++) {
2520                 ret = snd_soc_dapm_new_control(dapm, widget);
2521                 if (ret < 0) {
2522                         dev_err(dapm->dev,
2523                                 "ASoC: Failed to create DAPM control %s: %d\n",
2524                                 widget->name, ret);
2525                         return ret;
2526                 }
2527                 widget++;
2528         }
2529         return 0;
2530 }
2531 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2532
2533 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2534         const char *stream, int event)
2535 {
2536         struct snd_soc_dapm_widget *w;
2537
2538         list_for_each_entry(w, &dapm->card->widgets, list)
2539         {
2540                 if (!w->sname || w->dapm != dapm)
2541                         continue;
2542                 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2543                         w->name, w->sname, stream, event);
2544                 if (strstr(w->sname, stream)) {
2545                         switch(event) {
2546                         case SND_SOC_DAPM_STREAM_START:
2547                                 w->active = 1;
2548                                 break;
2549                         case SND_SOC_DAPM_STREAM_STOP:
2550                                 w->active = 0;
2551                                 break;
2552                         case SND_SOC_DAPM_STREAM_SUSPEND:
2553                         case SND_SOC_DAPM_STREAM_RESUME:
2554                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2555                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2556                                 break;
2557                         }
2558                 }
2559         }
2560
2561         dapm_power_widgets(dapm, event);
2562 }
2563
2564 /**
2565  * snd_soc_dapm_stream_event - send a stream event to the dapm core
2566  * @rtd: PCM runtime data
2567  * @stream: stream name
2568  * @event: stream event
2569  *
2570  * Sends a stream event to the dapm core. The core then makes any
2571  * necessary widget power changes.
2572  *
2573  * Returns 0 for success else error.
2574  */
2575 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2576         const char *stream, int event)
2577 {
2578         struct snd_soc_codec *codec = rtd->codec;
2579
2580         if (stream == NULL)
2581                 return 0;
2582
2583         mutex_lock(&codec->mutex);
2584         soc_dapm_stream_event(&codec->dapm, stream, event);
2585         mutex_unlock(&codec->mutex);
2586         return 0;
2587 }
2588
2589 /**
2590  * snd_soc_dapm_enable_pin - enable pin.
2591  * @dapm: DAPM context
2592  * @pin: pin name
2593  *
2594  * Enables input/output pin and its parents or children widgets iff there is
2595  * a valid audio route and active audio stream.
2596  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2597  * do any widget power switching.
2598  */
2599 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2600 {
2601         return snd_soc_dapm_set_pin(dapm, pin, 1);
2602 }
2603 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2604
2605 /**
2606  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2607  * @dapm: DAPM context
2608  * @pin: pin name
2609  *
2610  * Enables input/output pin regardless of any other state.  This is
2611  * intended for use with microphone bias supplies used in microphone
2612  * jack detection.
2613  *
2614  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2615  * do any widget power switching.
2616  */
2617 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2618                                   const char *pin)
2619 {
2620         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2621
2622         if (!w) {
2623                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2624                 return -EINVAL;
2625         }
2626
2627         dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2628         w->connected = 1;
2629         w->force = 1;
2630
2631         return 0;
2632 }
2633 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2634
2635 /**
2636  * snd_soc_dapm_disable_pin - disable pin.
2637  * @dapm: DAPM context
2638  * @pin: pin name
2639  *
2640  * Disables input/output pin and its parents or children widgets.
2641  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2642  * do any widget power switching.
2643  */
2644 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2645                              const char *pin)
2646 {
2647         return snd_soc_dapm_set_pin(dapm, pin, 0);
2648 }
2649 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2650
2651 /**
2652  * snd_soc_dapm_nc_pin - permanently disable pin.
2653  * @dapm: DAPM context
2654  * @pin: pin name
2655  *
2656  * Marks the specified pin as being not connected, disabling it along
2657  * any parent or child widgets.  At present this is identical to
2658  * snd_soc_dapm_disable_pin() but in future it will be extended to do
2659  * additional things such as disabling controls which only affect
2660  * paths through the pin.
2661  *
2662  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2663  * do any widget power switching.
2664  */
2665 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2666 {
2667         return snd_soc_dapm_set_pin(dapm, pin, 0);
2668 }
2669 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2670
2671 /**
2672  * snd_soc_dapm_get_pin_status - get audio pin status
2673  * @dapm: DAPM context
2674  * @pin: audio signal pin endpoint (or start point)
2675  *
2676  * Get audio pin status - connected or disconnected.
2677  *
2678  * Returns 1 for connected otherwise 0.
2679  */
2680 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2681                                 const char *pin)
2682 {
2683         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2684
2685         if (w)
2686                 return w->connected;
2687
2688         return 0;
2689 }
2690 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2691
2692 /**
2693  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2694  * @dapm: DAPM context
2695  * @pin: audio signal pin endpoint (or start point)
2696  *
2697  * Mark the given endpoint or pin as ignoring suspend.  When the
2698  * system is disabled a path between two endpoints flagged as ignoring
2699  * suspend will not be disabled.  The path must already be enabled via
2700  * normal means at suspend time, it will not be turned on if it was not
2701  * already enabled.
2702  */
2703 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2704                                 const char *pin)
2705 {
2706         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2707
2708         if (!w) {
2709                 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2710                 return -EINVAL;
2711         }
2712
2713         w->ignore_suspend = 1;
2714
2715         return 0;
2716 }
2717 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2718
2719 /**
2720  * snd_soc_dapm_free - free dapm resources
2721  * @card: SoC device
2722  *
2723  * Free all dapm widgets and resources.
2724  */
2725 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2726 {
2727         snd_soc_dapm_sys_remove(dapm->dev);
2728         dapm_debugfs_cleanup(dapm);
2729         dapm_free_widgets(dapm);
2730         list_del(&dapm->list);
2731 }
2732 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2733
2734 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2735 {
2736         struct snd_soc_dapm_widget *w;
2737         LIST_HEAD(down_list);
2738         int powerdown = 0;
2739
2740         list_for_each_entry(w, &dapm->card->widgets, list) {
2741                 if (w->dapm != dapm)
2742                         continue;
2743                 if (w->power) {
2744                         dapm_seq_insert(w, &down_list, false);
2745                         w->power = 0;
2746                         powerdown = 1;
2747                 }
2748         }
2749
2750         /* If there were no widgets to power down we're already in
2751          * standby.
2752          */
2753         if (powerdown) {
2754                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2755                 dapm_seq_run(dapm, &down_list, 0, false);
2756                 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2757         }
2758 }
2759
2760 /*
2761  * snd_soc_dapm_shutdown - callback for system shutdown
2762  */
2763 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2764 {
2765         struct snd_soc_codec *codec;
2766
2767         list_for_each_entry(codec, &card->codec_dev_list, list) {
2768                 soc_dapm_shutdown_codec(&codec->dapm);
2769                 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2770         }
2771 }
2772
2773 /* Module information */
2774 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2775 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2776 MODULE_LICENSE("GPL");