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